JavaSE-19.3.2【Properties集合和IO流相结合的方法】
1 package day10.lesson3; 2 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Properties; 7 8 /* 9 3.3 Properties和IO流相结合的方法 10 11 字节流 12 void load(InputStream inStream) 13 从输入字节流读取属性列表(键和元素对) 14 void store(OutputStreamout, String comments) 15 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用load(InputStream)方法的格式写入输出字节流 16 17 字符流 18 void load(Reader reader) 19 从输入字符流读取属性列表(键和元素对) 20 void store(Writer writer, String comments) 21 将此属性列表(键和元素对)写入此 Properties表中,以适合使用load(Reader)方法的格式写入输出字符流 22 */ 23 public class PropertiesDemo03 { 24 public static void main(String[] args) throws IOException{ 25 myStore(); 26 myLoad(); 27 } 28 29 private static void myStore() throws IOException { 30 Properties prop = new Properties(); 31 prop.setProperty("001", "zhangsan"); 32 prop.setProperty("002", "lisi"); 33 prop.setProperty("003", "wangwu"); 34 35 FileWriter fw = new FileWriter("stage2\\src\\day10\\lesson3\\fw.txt"); 36 prop.store(fw, null); 37 fw.close(); 38 } 39 40 private static void myLoad() throws IOException{ 41 Properties prop = new Properties(); 42 FileReader fr = new FileReader("stage2\\src\\day10\\lesson3\\fw.txt"); 43 prop.load(fr); 44 fr.close(); 45 46 System.out.println(prop); //{003=wangwu, 002=lisi, 001=zhangsan} 47 } 48 }
#Tue Jun 08 17:25:12 CST 2021 003=wangwu 002=lisi 001=zhangsan