IO 流之配置文件 properties文件

package com.qiang.IO6;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        Properties properties =new Properties();
        FileInputStream fileInputStream = null;

        FileOutputStream fileOutputStream = null;

        try {
            //创建输入流对象,用于指向磁盘中的属性文件路径
            fileInputStream = new FileInputStream("D:/Ideal wrok space/abc.properties");
            //根据一个输入流加载磁盘中某个属性文件所有的k/v结构到当前的properties对象中
            properties.load(fileInputStream);

            System.out.println(properties.get("name"));
            System.out.println(properties.get("age"));
            //根据可key获取属性文件中对应的值,如果key不存在返回参数2的数据,没有地址
            System.out.println(properties.getProperty("address", "没有地址"));
            //向properties对象中添加一个k/v结构,但是没有序列化到本次次磁盘
            properties.setProperty("address","南京");
            fileOutputStream = new FileOutputStream("D:/Ideal wrok space/abc.properties");
            //将当前properties对象序列化到磁盘某个属性文件中去
            properties.store(fileOutputStream,"属性文件");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream!= null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值