Java中的IO流(四)

  上一篇《Java中的IO流(三)》把IO流中的文件及目录操作的对象File类记录了一下,本篇把本不属性IO流但又和IO流有关系的一个对象作一下记录,此对象本属于集合框架里的一个子集,即Properties类

  Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。之所以把这个类放在IO流中来记录,原因就是因为此类可保存在流中或从流中加载。

  先记录一下Properties集合的特点:

    1,集合中的键和值都是字符串类型

    2,集合中的数据可以保存在流中,或从流中加载

  通常该集合用于操作以键值对形式存储的配置文件

一,Properties的基本操作-存-取-改

 1     private static void propertiesDemo1() {
 2         Properties prop = new Properties();
 3         // 往Properties中存数据用setProperties
 4         prop.setProperty("zw", "23");
 5         prop.setProperty("ls", "21");
 6         // 从Properties中取数据
 7         Set<String> set = prop.stringPropertyNames();// 获取Properties集合中所有的键的集合
 8         //循环读取键名合集中的所有键,然后用getProperty(键名)获取元素
 9         for (String str : set) {
10             System.out.println(prop.getProperty(str));
11         }
12         //修改元素用setProperty("键名相同","值不同");
13     }

二,Properties的集合方法-list(PrintSteam out)

  此方法的作用是将Properties集合中所存储的内容输出到所指定的输出流,即输出到所传递到list方法的参数out流,这个方法对调试很有用

1     private static void propertiesDemo2() {
2         Properties prop = new Properties();
3         // prop.setProperty("zw", "23");
4         // prop.setProperty("ls", "21");
5         prop = System.getProperties();// 获取系统的配置信息Properties集合
6         prop.list(System.out);
7     }

三,将集合中的内容持久化到文件中-store

  此方法有两个重载的版本,接收两个参数,第一个参数是流的类型;第二个参数是写信文件的注释信息,此注释信息不支持中文,若是中文则以uxxxx的形式写入

1     private static void propertiesDemo3() throws IOException {
2         Properties prop = System.getProperties();
3         FileWriter writer = new FileWriter("properties.properties");
4         prop.store(writer, "系统信息");
5     }

四,将文件中的信息读取到Properties集合中-load

  此文件中的信息必须是以键值对的形式存在的,此方法也有两个重载的版本,接收的参数流类型不一样

1     private static void propertiesDemo4() throws IOException {
2         Properties prop = new Properties();
3         Reader reader = new FileReader("properties.properties");
4         prop.load(reader);//加载文件到集合中
5         prop.list(System.out);//把集合中的所有内容输出到控制台
6     }

五,将键值对的集合数据持久化成XML形式的文件-storeToXML

  此方法有两个重载的版本接收三个参数的版本可以指定编码格式  

1     private static void propertiesDemo5() throws IOException {
2         Properties prop = System.getProperties();
3         OutputStream outputStream = new FileOutputStream("a.xml");
4         prop.storeToXML(outputStream, "这是一个XML文件", "utf-8");
5     }

六,将XMl类型的配置文件读取到集合中

1   private static void propertiesDemo6() throws InvalidPropertiesFormatException, IOException {
2         Properties prop = new Properties();
3         InputStream inputStream = new FileInputStream("a.xml");
4         prop.loadFromXML(inputStream);
5         prop.list(System.out);
6     }

 七,读取配置文件的信息并修改其中的内容然后存储该修改后的配置文件信息

 1     private static void propertiesDemo7() throws IOException {
 2         Properties prop = new Properties();
 3         File file = new File("properties.properties");
 4         if (!file.exists()) {
 5             file.createNewFile();
 6         }
 7         Reader reader = new FileReader(file);
 8         prop.load(reader);
 9         Set<String> set = prop.stringPropertyNames();
10         for (String str : set) {
11             if (str.equals("os.version")) {
12                 prop.setProperty(str, "8.0");
13             }
14         }
15         prop.store(new FileWriter("properties.properties"), "new config");
16     }

 八,模拟软件试用的功能,即试用五次,然后提示用户注册

 1     private static void propertiesDemo8() throws IOException {
 2         File file = new File("config.properties");
 3         if (!file.exists()) {
 4             file.createNewFile();
 5         }
 6         Reader inputStream = new FileReader(file);
 7         Properties properties = new Properties();
 8         properties.load(inputStream);
 9         Writer writer = new FileWriter(file);
10         String count = properties.getProperty("count");
11         try {
12             if (count == null || count.equals("")) {
13                 properties.setProperty("count", "0");
14                 properties.store(writer, "First time");
15             } else {
16                 int countTemp = Integer.parseInt(count);
17                 countTemp++;
18                 properties.setProperty("count", String.valueOf(countTemp));
19                 properties.store(writer, "new time ");
20                 if (countTemp >= 5) {
21                     throw new RuntimeException("试用次数已到,请注册");
22                 }
23 
24             }
25         } catch (RuntimeException e) {
26             e.printStackTrace();
27         } finally {
28             inputStream.close();
29             writer.close();
30         }
31     }
原文地址:https://www.cnblogs.com/zw971084570/p/10045522.html