一般使用到properties配置文件,一般都是在spring項目里面,直接由框架幫你讀,當然,你也得考慮到編碼的問題。
但是現在要是要求使用java直接讀寫properties文件,就發現很多的問題,比如,我的properties文件的編碼竟然不是utf-8的?;蛘哒f我壓根就沒考慮到這個問題。
再比如,當properties文件里面有漢子的時候,發現讀寫的漢字亂碼了,在我這是因為我的電腦默認編碼是gbk,但是讀的時候,又沒有設置編碼,搞出的問題。
下面直接上代碼,看問題。
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
|
package com.lxk.propertyfiletest; import java.io.*; import java.util.properties; /** * 讀寫properties文件測試 * <p> * created by lxk on 2017/4/25 */ public class main { public static void main(string[] args) { properties prop = new properties(); inputstream in = null ; fileoutputstream ofile = null ; try { in = new bufferedinputstream( new fileinputstream( "d:config.properties" )); //prop.load(in);//直接這么寫,如果properties文件中有漢子,則漢字會亂碼。因為未設置編碼格式。 prop.load( new inputstreamreader(in, "utf-8" )); for (string key : prop.stringpropertynames()) { system.out.println(key + ":" + prop.getproperty(key)); } //保存屬性到b.properties文件 ofile = new fileoutputstream( "b.properties" , false ); //true表示追加打開,false每次都是清空再重寫 prop.setproperty( "phone" , "10086" ); //prop.store(ofile, "此參數是保存生成properties文件中第一行的注釋說明文字");//這個會兩個地方亂碼 //prop.store(new outputstreamwriter(ofile, "utf-8"), "漢字亂碼");//這個就是生成的properties文件中第一行的注釋文字亂碼 prop.store( new outputstreamwriter(ofile, "utf-8" ), "lll" ); } catch (exception e) { system.out.println(e.getmessage()); } finally { if (in != null ) { try { in.close(); } catch (ioexception e) { system.out.println(e.getmessage()); } } if (ofile != null ) { try { ofile.close(); } catch (ioexception e) { system.out.println(e.getmessage()); } } } } } |
運行結果:這個只是讀出來的內容的結果。
下面是寫出來的文件內容。
額,這個圖,有點亂。但是,卻把三種運行情況,全部給展示出來了。很清晰。
最后,代碼里面也看到了怎么把字節流變成帶編碼格式的字符流,這個可以注意下,我也留個筆記。
對上面的代碼的更新,算是結構調整,功能分開。瞬間代碼看著就清晰明了啦。
所以,一般上面的代碼是不推薦實用的。個中妙用,自行體會吧。
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
|
package com.lxk.propertyfiletest; import java.io.*; import java.util.properties; /** * 讀寫properties文件測試 * <p> * created by lxk on 2017/4/25 */ public class main { public static void main(string[] args) { properties prop = readpropertiesfile(); writepropertiesfile(prop); } /** * 讀properties文件 */ private static properties readpropertiesfile() { properties prop = new properties(); inputstream in = null ; try { in = new bufferedinputstream( new fileinputstream( "d:config.properties" )); //prop.load(in);//直接這么寫,如果properties文件中有漢子,則漢字會亂碼。因為未設置編碼格式。 prop.load( new inputstreamreader(in, "utf-8" )); for (string key : prop.stringpropertynames()) { system.out.println(key + ":" + prop.getproperty(key)); } } catch (exception e) { system.out.println(e.getmessage()); } finally { if (in != null ) { try { in.close(); } catch (ioexception e) { system.out.println(e.getmessage()); } } } return prop; } /** * 寫properties文件 */ private static void writepropertiesfile(properties prop) { prop.setproperty( "phone" , "10086" ); fileoutputstream ofile = null ; try { //保存屬性到b.properties文件 ofile = new fileoutputstream( "b.properties" , false ); //true表示追加打開,false每次都是清空再重寫 //prop.store(ofile, "此參數是保存生成properties文件中第一行的注釋說明文字");//這個會兩個地方亂碼 //prop.store(new outputstreamwriter(ofile, "utf-8"), "漢字亂碼");//這個就是生成的properties文件中第一行的注釋文字亂碼 prop.store( new outputstreamwriter(ofile, "utf-8" ), "lll" ); } catch (exception e) { system.out.println(e.getmessage()); } finally { if (ofile != null ) { try { ofile.close(); } catch (ioexception e) { system.out.println(e.getmessage()); } } } } } |
注意:這個是我后來發現的,不知道在看的各位有沒有這個問題。
我發現寫出來的properties文件的編碼格式并不是簡單的utf-8,而是utf-8無bom格式。證據可參見下圖:
這個打開工具叫 notepad++ 估計在看的各位的電腦上都有這個吧。
但是你要是把這個文件的編碼格式給修改成utf-8編碼之后,運行的結果,就有一丟丟不一樣。
繼續參見下圖:
看到多了一個小杠“”-“”,具體怎么解釋,我暫時還不清楚。
這個時候,寫出來的文件的,也同樣出現了這個問題,具體還是繼續參見下圖:
所以,這個我暫時解釋不了。
慚愧。。。。
還有個問題就是:讀出來的屬性,是不按原來文件中的順序展示的,當然寫的時候,也是亂序的。
這還是個問題,還有待解決。什么時候解決了,再在此處留個鏈接。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_27093465/article/details/70765870