我是靠谱客的博主 忐忑小蘑菇,这篇文章主要介绍Java学习笔记之IO(十四):Properties配置文件类,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.io.g_sequence; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import org.junit.Test; /* Properties:配置文件类,主要用于产生配置文件和读取配置文件的信息; Properties是属于Map集合体系的类; 因为该类继承于Hashtable,所以可以使用put()和putAll()方法, 但是不建议使用;因为这两个方法可以添加任意类型的数据,而Properties用来操作的配置文件中都是字符串数据, 所以建议使用setProperty(String, String)方法; Properties注意事项: 1、如果配置文件的信息使用了中文,那么在使用 store() 方法生成配置文件的时候,只能使用字符流方式;如果使用 字节流生成配置文件,默认使用的是ISO8859-1进行编码存储,会出现乱码; 2、如果Properties中的内容发生了变化,要重新调用store()方法将数据更新到配置文件中; */ public class Demo4 { // Properties属于Map集合体系的基本遍历方式 @Test public void test1(){ // 创建Properties对象 Properties prop = new Properties(); prop.setProperty("张三", "123"); prop.setProperty("李四", "234"); prop.setProperty("王五", "345"); // 遍历Properties Set<Entry<Object, Object>> entrySet = prop.entrySet(); for (Entry<Object, Object> entry : entrySet){ System.out.println(entry.getKey() + "---" + entry.getValue()); } } // 使用 Properties 生成配置文件,并使用字节流方法将数据写入到配置文件中 @Test public void test2() throws IOException{ // 创建Properties对象 Properties prop = new Properties(); prop.setProperty("Jack", "123"); prop.setProperty("Lucy", "234"); prop.setProperty("Lily", "345"); // 找到目标配置文件 File file = new File("D:\user.properties"); // 创建数据输出流对象 FileOutputStream out = new FileOutputStream(file); // 生成配置文件;参数1:配置文件的输出流对象;参数2:使用一个字符串描述配置文件的信息; prop.store(out, "user"); } /* 使用 Properties 生成配置文件,并使用字节流将中文数据写入到配置文件中,结果为: #user #Wed Oct 11 16:48:08 CST 2017 u738Bu4E94=345 u5F20u4E09=123 u674Eu56DB=234 查看 store() 方法的源码可以看出,该方法默认使用的是 ISO8859-1 编码方式,该编码方式不支持中文; 解决办法:可以使用字符流写入中文数据; */ @Test public void test3() throws IOException{ // 创建Properties对象 Properties prop = new Properties(); prop.setProperty("张三", "123"); prop.setProperty("李四", "234"); prop.setProperty("王五", "345"); // 找到目标配置文件 File file = new File("D:\user.properties"); // 创建数据输出字节流对象 FileOutputStream out = new FileOutputStream(file); // 生成配置文件; 参数1:配置文件的输出字节流对象; 参数2:使用一个字符串描述配置文件的信息; prop.store(out, "user"); } // 使用 字符流 向配置文件中写入 中文 @Test public void test4() throws IOException{ // 创建Properties对象 Properties prop = new Properties(); prop.setProperty("张三", "123"); prop.setProperty("李四", "234"); prop.setProperty("王五", "345"); // 找到目标配置文件 File file = new File("D:\user.properties"); // 创建数据输出字节流对象 FileWriter writer = new FileWriter(file); // 生成配置文件;参数1:配置文件的输出字符流对象;参数2:使用一个字符串描述配置文件的信息; prop.store(writer, "user"); } // 读取配置文件数据;由于Properties是一个容器,所以可以直接把数据加载到该容器中; @Test public void test5() throws IOException{ // 创建Properties对象 Properties prop = new Properties(); // 找到目标文件 File file = new File("D:\user.properties"); // 建立数据输入字符流 FileReader reader = new FileReader(file); // 加载配置文件到Properties容器中;参数是一个输入流对象; prop.load(reader); // 如果知道键,可以直接根据键获取值 System.out.println("张三:" + prop.getProperty("张三"));; System.out.println("李四:" + prop.getProperty("李四"));; System.out.println("王五:" + prop.getProperty("王五"));; // 遍历集合容器 Set<Entry<Object, Object>> entrys = prop.entrySet(); for (Entry<Object, Object> entry : entrys){ System.out.println(entry.getKey() + "---" + entry.getValue()); } // 修改配置文件数据;此处修改的只是集合容器中的数据,并没有更新到配置文件中; prop.setProperty("张三", "007"); // 将修改之后的集合容器中的数据重新写入配置文件中 prop.store(new FileWriter(file), "user"); } }

最后

以上就是忐忑小蘑菇最近收集整理的关于Java学习笔记之IO(十四):Properties配置文件类的全部内容,更多相关Java学习笔记之IO(十四)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部