Java中讀寫資源文件最重要的類是Properties
1) 資源文件要求如下:
1、properties文件是一個文本文件
2、properties文件的語法有兩種,一種是注釋,一種屬性配置。
注??? 釋:前面加上#號
屬性配置:以“鍵=值”的方式書寫一個屬性的配置信息。
3、properties文件的一個屬性配置信息值可以換行,但鍵不可以換行。值換行用“\”表示。
4、properties的屬性配置鍵值前后的空格在解析時候會被忽略。
5、properties文件可以只有鍵而沒有值。也可以僅有鍵和等號而沒有值,但無論如何一個屬性配置不能沒有鍵。
eg:
正確的資源文件格式為:
2) 功能大致如下:
1. 讀寫Properties文件
2. 讀寫XML文件
3. 不僅可以讀寫上述兩類文件,還可以讀寫其它格式文件如txt等,只要符合key=value格式即可.
Properties能讀取以key,value存儲的任何格式文件,看一下他的類結構就知道為什么了
從上面的類結構圖可以看出,它繼承了Hashtable并實現了Map接口
3)代碼演示:
1 packagecom.ifly.myhome.test;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.InputStream;8 importjava.io.OutputStream;9 importjava.io.UnsupportedEncodingException;10 importjava.util.Properties;11
12 public classPropertiesMyTest13 {14
15 public static voidmain(String[] args)16 {17
18 String readfile = "e:" + File.separator + "readfile.properties";19 String writefile = "e:" + File.separator + "writefile.properties";20 String readxmlfile = "e:" + File.separator + "readxmlfile.xml";21 String writexmlfile = "e:" + File.separator + "writexmlfile.xml";22 String readtxtfile = "e:" + File.separator + "readtxtfile.txt";23 String writetxtfile = "e:" + File.separator + "writetxtfile.txt";24
25 readPropertiesFile(readfile); //讀取properties文件
26 writePropertiesFile(writefile); //寫properties文件
27 readPropertiesFileFromXML(readxmlfile); //讀取XML文件
28 writePropertiesFileToXML(writexmlfile); //寫XML文件
29 readPropertiesFile(readtxtfile); //讀取txt文件
30 writePropertiesFile(writetxtfile); //寫txt文件
31 }32
33 //讀取資源文件,并處理中文亂碼
34 public static voidreadPropertiesFile(String filename)35 {36 Properties properties = newProperties();37 try
38 {39 InputStream inputStream = newFileInputStream(filename);40 properties.load(inputStream);41 inputStream.close(); //關閉流
42 }43 catch(IOException e)44 {45 e.printStackTrace();46 }47 String username = properties.getProperty("username");48 String passsword = properties.getProperty("password");49 String chinese = properties.getProperty("chinese");50 try
51 {52 chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); //處理中文亂碼
53 }54 catch(UnsupportedEncodingException e)55 {56 e.printStackTrace();57 }58 System.out.println(username);59 System.out.println(passsword);60 System.out.println(chinese);61 }62
63 //讀取XML文件,并處理中文亂碼
64 public static voidreadPropertiesFileFromXML(String filename)65 {66 Properties properties = newProperties();67 try
68 {69 InputStream inputStream = newFileInputStream(filename);70 properties.loadFromXML(inputStream);71 inputStream.close();72 }73 catch(IOException e)74 {75 e.printStackTrace();76 }77 String username = properties.getProperty("username");78 String passsword = properties.getProperty("password");79 String chinese = properties.getProperty("chinese"); //XML中的中文不用處理亂碼,正常顯示
80 System.out.println(username);81 System.out.println(passsword);82 System.out.println(chinese);83 }84
85 //寫資源文件,含中文
86 public static voidwritePropertiesFile(String filename)87 {88 Properties properties = newProperties();89 try
90 {91 OutputStream outputStream = newFileOutputStream(filename);92 properties.setProperty("username", "myname");93 properties.setProperty("password", "mypassword");94 properties.setProperty("chinese", "中文");95 properties.store(outputStream, "author: shixing_11@sina.com");96 outputStream.close();97 }98 catch(IOException e)99 {100 e.printStackTrace();101 }102 }103
104 //寫資源文件到XML文件,含中文
105 public static voidwritePropertiesFileToXML(String filename)106 {107 Properties properties = newProperties();108 try
109 {110 OutputStream outputStream = newFileOutputStream(filename);111 properties.setProperty("username", "myname");112 properties.setProperty("password", "mypassword");113 properties.setProperty("chinese", "中文");114 properties.storeToXML(outputStream, "author: shixing_11@sina.com");115 outputStream.close();116 }117 catch(IOException e)118 {119 e.printStackTrace();120 }121 }122
123 }
View Code
運行本程序所需的資源文件,我是放在E盤根目錄,如E:/readfile.properties
1. readfile.properties
username=kh
password=kh
chinese=謂語
2.?writefile.properties
#author: shixing_11@sina.com
#Fri May 28 22:19:44 CST 2010
password=kh
chinese=\u8C13\u8BED
username=kh
3.?readxmlfile.xml
mypassword
中文
myname
4.?writexmlfile.xml
kh
中文
kh
5. readtxtfile.txt
username=kh
password=kh
chinese=中文
6.?writetxtfile.txt
password=kh
chinese=/u4E2D/u6587
username=kh
4)Properties獲取數據亂碼解決
1.原因
Properties調用load(InputStream)時,讀取文件時使用的默認編碼為ISO-8859-1;當我們講中文放入到properties文件中,通過getProperty(key)獲取值時,取到得數據是ISO-8859-1格式的,但是ISO-8859-1是不能識別中文的。
2.解決方法
通過getProperty()獲取的數據data既然是ISO-8859-1編碼的,就通過data.getByte(“iso-8859-1”)獲取獲取,使用new String(data.getByte(“iso-8859-1”),”UTF-8”)進行轉換。當然properties文件的編碼類型需要和new String(Byte[],charst)中的第二個參數的編碼類型相同