Java IO流之Properties类


Properties类 属性集与IO流结合

java.util.Properties继承于Hashtable ,来表示一个持久的属性集。它使用简直结构存储数据,每一个键及其对应的值都是一个字符串。该类也被许多java类使用,比如获取系统属性,System.getProperties方法就是返回一个Properties对象。

Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。唯一和IO流相结合的集合

  • 可以使用Properties的方法store,将集合中的临时数据,持久化存储到硬盘文件中
  • 可以使用Properties的方法load,将硬盘中文件保存的键值对,读取到集合中使用

始于:JDK 1.0


1、构造方法

  • public Properties()创建一个空的属性列表。
  • public Properties(Properties default)创建一个具有指定默认值的空属性列表。

2、常用方法

  • public Object setProperties(String key, String value) 保存一对属性

  • public String getProperties(String key) 使用此属性列表中指定的键搜索属性值

  • public Set<String> stringPropertiesName() 所有键的名称集合

  • public void load(InputStream inStream) throws IOException 将硬盘中的数据通过字节IO流加载到集合中

  • public void load(Reader reader) throws IOException 将硬盘中的数据通过字符IO流加载到集合中

  • pubilc void store(OutputStream outStream, String comments) throws IOException 将集合中的数据通过字节IO流输出存入到硬盘文件中。第二个参数为注释,给该存储给一个注释(不能使用中文,会乱码,默认UNICODE编码),一般使用空字符串

  • public void store(Writer writer, String comments) throws IOException 将集合中的数据通过字符IO流输出存入到硬盘文件中。第二个参数为注释,给该存储给一个注释(不能使用中文,会乱码,默认UNICODE编码),一般使用空字符串

public class PropertiesTest {
    public static void main(String[] args){
        ioInProperties();
    }

    public static void forProperties(){
        // 向Properties存储数据
        Properties properties = new Properties();
        properties.setProperty("heroC","1130");
        properties.setProperty("yikeX","0405");
        properties.setProperty("heroCheung","2019");
        // 遍历Properties
        Set<String> strings = properties.stringPropertyNames();
        for (String s : strings){
            System.out.println("key " + s + "---" + "value " + properties.getProperty(s) );
        }
    }

    public static void ioOutProperties(){
        // 方法store有两个,字节流存储(不能读取中文)和字符流存储
        // 存储到硬盘中是采用unicode编码
        try(FileOutputStream fileOutputStream = new FileOutputStream("d.txt")){
            Properties properties = new Properties();
            properties.setProperty("heroC","1130");
            properties.setProperty("yikeX","0405");
            properties.store(fileOutputStream,"");
        }catch (IOException e){
            System.out.println("IO有异常");
        }
    }

    public static void ioInProperties(){
        // 方法load有两个,字节流读取(不能读取中文)和字符流读取
        try(FileReader fileReader = new FileReader("d.txt")){
            Properties properties = new Properties();
            properties.load(fileReader);
            Set<String> strings = properties.stringPropertyNames();
            for (String key : strings) {
                System.out.println(key + "=" + properties.get(key));
            }
        }catch (IOException e){
            System.out.println("IO有异常");
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值