属性集合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();
- }
- }
复制代码
|
|