Java代码实现对properties文件有序的读写的示例

2025-05-29 0 24

最近遇到一项需求,要求把properties文件中的内容读取出来供用户修改,修改完后需要再重新保存到properties文件中。很简单的需求吧,可问题是properties是继承自hashtable的,直接通过keyset()、keys()或entryset()方法对properties中的元素进行遍历时取出来的内容顺序与properties文件中的顺序不一致,这是问题一;问题二是就算取出来的时候是有序的,保存到文件中时又是无序的了。

当然,解决这两个问题的方法有很多。我最终采用的方法是自定义一个propertiesutil类,该类继承自propertiespropertiesutil提供一个返回由key按照存入顺序组成的list的方法,getkeylist(),这样问题一就解决了。那如何保证getkeylist()方法返回的就是有序的key组成的集合呢?我查看了一下properties方法的源码,发现其setproperty()方法实际上就是调用了父类hashtable的put()方法,其次properties在从文件中加载内容时是按照文件顺序进行读取,然后调用父类hashtable的put()方法进行储存。所以问题的解决办法就是propertiesutil持有一个私有的可以有序存储key的集合,然后重写父类的put()方法,在方法体中照常通过super.put()进行属性的存储,同时将key添加到存储key的集合中。

properties提供有save()方法和store()方法可以将当前对象的内容存放到指定的输出流中,但它们的底层逻辑都是一样的。通过调用keys()方法获取一个enumeration,然后对该enumeration进行遍历,依次将对应的key和value写入到输出流中,所以要保证写入是有序的,就要保证遍历keys()返回的enumeration时取出的元素key是有序的。所以解决方法是重写keys()方法,保证遍历返回的enumeration时得到的key是有序的。

下面就示范怎么按顺序读properties文件,以及还得按原来的顺序写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
package com.lxk.propertyfiletest;

import java.util.*;

/**

* created by lxk on 2017/5/2

*/

public class orderedproperties extends properties {

private static final long serialversionuid = -4627607243846121965l;

/**

* 因为linkedhashset有序,所以,key在调用put()的时候,存放到这里也就有序。

*/

private final linkedhashset<object> keys = new linkedhashset<>();

@override

public enumeration<object> keys() {

return collections.enumeration(keys);

}

/**

* 在put的时候,只是把key有序的存到{@link orderedproperties#keys}

* 取值的时候,根据有序的keys,可以有序的取出所有value

* 依然调用父类的put方法,也就是key value 键值对还是存在hashtable里.

* 只是现在多了个存key的属性{@link orderedproperties#keys}

*/

@override

public object put(object key, object value) {

keys.add(key);

return super.put(key, value);

}

/**

* 因为复写了这个方法,在(方式一)的时候,才输出有序。

* {@link mainorder#printprop}

*/

@override

public set<string> stringpropertynames() {

set<string> set = new linkedhashset<>();

for (object key : this.keys) {

set.add((string) key);

}

return set;

}

/**

* 因为复写了这个方法,在(方式二)的时候,才输出有序。

* {@link mainorder#printprop}

*/

@override

public set<object> keyset() {

return keys;

}

//这个就不设置有序了,因为涉及到hashtable内部类:entryset,不好复写。

//public linkedhashset<map.entry<object, object>> entryset() {

// linkedhashset<map.entry<object, object>> entryset = new linkedhashset<>();

// for (object key : keys) {

//

// }

// return entryset;

//}

/**

* 因为复写了这个方法,在(方式四)的时候,才输出有序。

* {@link mainorder#printprop}

*/

@override

public enumeration<?> propertynames() {

return collections.enumeration(keys);

}

}

上面是继承java自带的类,我们做的主要是实现有序,其他的还是原来的样子就行。

看下整个的类继承关系:如下图:

Java代码实现对properties文件有序的读写的示例

下面是main方法的类。

?

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
package com.lxk.propertyfiletest;

import java.io.*;

import java.util.enumeration;

import java.util.map;

import java.util.properties;

import java.util.set;

/**

* 读写properties文件测试(带顺序的读和写)

* <p>

* created by lxk on 2017/5/2

*/

public class mainorder {

public static void main(string[] args) {

properties prop = readorderedpropertiesfile();

printprop(prop);

writeorderedpropertiesfile(prop);

}

/**

* 输出properties的key和value

*/

public static void printprop(properties properties) {

system.out.println("---------(方式一)------------");

for (string key : properties.stringpropertynames()) {

system.out.println(key + "=" + properties.getproperty(key));

}

system.out.println("---------(方式二)------------");

set<object> keys = properties.keyset();//返回属性key的集合

for (object key : keys) {

system.out.println(key.tostring() + "=" + properties.get(key));

}

system.out.println("---------(方式三)------------");

set<map.entry<object, object>> entryset = properties.entryset();//返回的属性键值对实体

for (map.entry<object, object> entry : entryset) {

system.out.println(entry.getkey() + "=" + entry.getvalue());

}

system.out.println("---------(方式四)------------");

enumeration<?> e = properties.propertynames();

while (e.hasmoreelements()) {

string key = (string) e.nextelement();

string value = properties.getproperty(key);

system.out.println(key + "=" + value);

}

}

/**

* 读properties文件(有序)

*/

private static properties readorderedpropertiesfile() {

properties properties = new orderedproperties();

inputstreamreader inputstreamreader = null;

try {

inputstream inputstream = new bufferedinputstream(new fileinputstream("d:testorder.properties"));

//prop.load(in);//直接这么写,如果properties文件中有汉子,则汉字会乱码。因为未设置编码格式。

inputstreamreader = new inputstreamreader(inputstream, "utf-8");

properties.load(inputstreamreader);

} catch (exception e) {

system.out.println(e.getmessage());

} finally {

if (inputstreamreader != null) {

try {

inputstreamreader.close();

} catch (ioexception e) {

system.out.println(e.getmessage());

}

}

}

return properties;

}

/**

* 写properties文件(有序)

*/

private static void writeorderedpropertiesfile(properties properties) {

properties.setproperty("phone", "10086");

outputstreamwriter outputstreamwriter = null;

try {

//保存属性到b.properties文件

fileoutputstream fileoutputstream = new fileoutputstream("order.properties", false);//true表示追加打开,false每次都是清空再重写

//prop.store(ofile, "此参数是保存生成properties文件中第一行的注释说明文字");//这个会两个地方乱码

//prop.store(new outputstreamwriter(ofile, "utf-8"), "汉字乱码");//这个就是生成的properties文件中第一行的注释文字乱码

outputstreamwriter = new outputstreamwriter(fileoutputstream, "utf-8");

properties.store(outputstreamwriter, "lll");

} catch (exception e) {

system.out.println(e.getmessage());

} finally {

if (outputstreamwriter != null) {

try {

outputstreamwriter.close();

} catch (ioexception e) {

system.out.println(e.getmessage());

}

}

}

}

}

其实读和写,都和使用系统提供的类的差别不大,只是现在读到了我们自己写的子类里面去了。

其他的代码都是一样样的。

下面是读的文件的内容截图:

Java代码实现对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
---------(方式一)------------

1=11

2=22

3=33

4=44

5=55

6=66

7=77

8=88

9=99

10=18

11汉字=测试汉字以防乱码产生

---------(方式二)------------

1=11

2=22

3=33

4=44

5=55

6=66

7=77

8=88

9=99

10=18

11汉字=测试汉字以防乱码产生

---------(方式三)------------

11汉字=测试汉字以防乱码产生

9=99

8=88

7=77

6=66

5=55

4=44

3=33

2=22

10=18

1=11

---------(方式四)------------

1=11

2=22

3=33

4=44

5=55

6=66

7=77

8=88

9=99

10=18

11汉字=测试汉字以防乱码产生

额,太长了,就不截图了吧,就给把打印结果给展示一下得了。

可以看到,只有第三次是无序的,具体原因,我也在代码里面解释过了。

还有,就是生成的文件的截图:

Java代码实现对properties文件有序的读写的示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/qq_27093465/article/details/71108520

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java代码实现对properties文件有序的读写的示例 https://www.kuaiidc.com/113808.html

相关文章

发表评论
暂无评论