Properties

1
2
3
4
5
6
7
8
9
10
11
12
Properties 属性集对象
属于Map集合
作用:可以把键值对数据存入到一个属性文件

api
setProperty(_, _) 也就是put(_, _)
getProperty(_)
stringPropertyNames() 所有键的名称的集合
store(OutputStream out, String comments) 保存数据到属性文件
store(Writer fw, String comments) 保存数据到属性文件
synchronized void load(InputStream is)
synchronized void load(Reader fr)
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
/**
* Created by KebabShell
* on 2020/4/11 下午 04:22
*/
public class PropertiesTest {
/**
* 存
* @throws Exception
*/
@Test
public void test0() throws Exception{
Properties properties = new Properties();
properties.setProperty("name", "kb");
System.out.println(properties);

FileOutputStream fos = new FileOutputStream("src/cn/kebabshell/test/util/properties/test.properties");

properties.store(fos, "this is comment");

// fos.close();
// store会自动关闭fos
}

/**
* 取
* @throws Exception
*/
@Test
public void test1() throws Exception{
Properties properties = new Properties();

FileInputStream fis = new FileInputStream("src/cn/kebabshell/test/util/properties/test.properties");

properties.load(fis);

System.out.println(properties);

// System.out.println(properties.get("name"));
System.out.println(properties.getProperty("name"));
}
@Test
public void test2() throws Exception{
Properties properties = new Properties();

FileReader fr = new FileReader("src/cn/kebabshell/test/util/properties/test.properties");

properties.load(fr);

System.out.println(properties);

// System.out.println(properties.get("name"));
System.out.println(properties.getProperty("name"));

}
}