黑马程序员技术交流社区
标题:
属性集合Properties使用方法
[打印本页]
作者:
hero_king
时间:
2016-6-1 22:16
标题:
属性集合Properties使用方法
属性集合Properties使用方法,自己写的
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.Set;
/*
* 属性集合Properties的使用
* 将String类型的键值对保存到文件,将文件中的键值对字符串读取到集合中
*/
public class Demo8 {
public static void main(String[] args) throws IOException {
store();
load();
}
private static void load() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("玩家信息.txt");
prop.load(fis);
Set<String> set = prop.stringPropertyNames();
for (String s : set) {
String value = prop.getProperty(s);
System.out.println(s + value);
}
}
private static void store() throws IOException {
Properties prop = new Properties();
prop.setProperty("吕布", "1");
prop.setProperty("方天画戟", "1");
prop.setProperty("貂蝉", "1");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("玩家信息.txt"), "gbk"));
prop.store(bw, "三国志");
bw.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2