Properties持久化键值对

Properties
特点:
1.HashTable的子类,map集合中的方法都可以用。
2.该集合没有泛型,键值都是字符串。
3.它是一个可以持久化的属性集,键值可以存储到集合中,也可以存储到持久化设备上
键值的来源也可以是持久化设备。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyDemo {

    public static void main(String[] args) throws IOException {
        
        /*
         * 演示一下Properties的特有方法
         */
//        methodDemo();
        
        methodDemo2();
    }
    
    public static void methodDemo2() throws IOException{
        
        Properties prop = new Properties();
        
        prop.setProperty("zhangsan", "20");
        prop.setProperty("lisi", "23");
        prop.setProperty("wangwu", "10");
        
        //将集合中的数据持久化到设备上
        //需要输出流对象
        FileOutputStream fos = new FileOutputStream("tempfile\info.properties");
        
        //使用prop的store方法,
        prop.store(fos, "my demo person info");//store需要用到输出流
        
        fos.close();
        
    }
    
    public static void methodDemo(){
        
        //Properties的基本存和取
        
        //1.创建一个Properties
        Properties prop = new Properties();
        
        prop.setProperty("zhangsan", "20");
        prop.setProperty("lisi", "23");
        prop.setProperty("wangwu", "10");
        
        prop.list(System.out);//此方法对调试很有用
        
//        Set<String> set = prop.stringPropertyNames();
//        
//        for(String name:set){
//            String value = prop.getProperty(name);
//            System.out.println(name+"...."+value);
//        }
    }

}
原文地址:https://www.cnblogs.com/qjlbky/p/5905689.html