java Properties

 

txt文件操作

// txt文件操作
Properties prop = new Properties();
String s = "Height=200";
String s2 = "Width=15";
FileOutputStream fos = new FileOutputStream("properties.txt");
fos.write(s.getBytes());
fos.write("\r\n".getBytes());
fos.write(s2.getBytes());

FileInputStream fis = new FileInputStream("properties.txt");
prop.load(fis); // txt:从输入字节流读取
prop.list(System.out);
System.out.println(prop.get("Height"));

 

xml文件操作

// xml文件操作
Properties prop = new Properties();
prop.put("Height", "200");
prop.put("Width", "15");
FileOutputStream fos = new FileOutputStream("properties.xml");
prop.storeToXML(fos, "Properties Example"); // xml:输出流写入

FileInputStream fis = new FileInputStream("properties.xml");
prop.loadFromXML(fis); // xml:从输入流读取properties
prop.list(System.out);

Set<String> set = prop.stringPropertyNames();
System.out.println(set)

 

posted @ 2018-06-14 20:24  草木物语  阅读(400)  评论(0编辑  收藏  举报