properties類讀取.properties 、簡單XML文件、txt文件

Java中讀寫資源文件最重要的類是Properties,功能大致如下:
1. 讀寫Properties文件
2. 讀寫XML文件
3. 不僅可以讀寫上述兩類文件,還可以讀寫其它格式文件如txt等,只要符合key=value格式即可.

注意:資源文件中含有中文時的處理方法 
1. 將中文字符通過工作轉成utf8編碼,可以通過Java自帶的nativetoascii或Eclipse中的屬性編輯器。
2. 直接調用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");

附:WEB程序中加載資源文件的方法
Properties prop = null; 
1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");

Properties能讀取以key,value存儲的任何格式文件,究竟有什麼神奇,貓一眼類結構,
Properties類結構
原來它繼承了Hashtable並實現了Map接口,這樣大家放心了吧。

 

  1. package apistudy;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.io.UnsupportedEncodingException;  
  10. import java.util.Properties;  
  11.   
  12. public class PropertiesTest  
  13. {  
  14.   
  15.     public static void main(String[] args)  
  16.     {  
  17.   
  18.         String readfile = "d:" + File.separator + "readfile.properties";  
  19.         String writefile = "d:" + File.separator + "writefile.properties";  
  20.         String readxmlfile = "d:" + File.separator + "readxmlfile.xml";  
  21.         String writexmlfile = "d:" + File.separator + "writexmlfile.xml";  
  22.         String readtxtfile = "d:" + File.separator + "readtxtfile.txt";  
  23.         String writetxtfile = "d:" + 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 void readPropertiesFile(String filename)  
  35.     {  
  36.         Properties properties = new Properties();  
  37.         try  
  38.         {  
  39.             InputStream inputStream = new FileInputStream(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 void readPropertiesFileFromXML(String filename)  
  65.     {  
  66.         Properties properties = new Properties();  
  67.         try  
  68.         {  
  69.             InputStream inputStream = new FileInputStream(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 void writePropertiesFile(String filename)  
  87.     {  
  88.         Properties properties = new Properties();  
  89.         try  
  90.         {  
  91.             OutputStream outputStream = new FileOutputStream(filename);  
  92.             properties.setProperty("username""myname");  
  93.             properties.setProperty("password""mypassword");  
  94.             properties.setProperty("chinese""中文");  
  95.             properties.store(outputStream, "author: [email protected]");  
  96.             outputStream.close();  
  97.         }  
  98.         catch (IOException e)  
  99.         {  
  100.             e.printStackTrace();  
  101.         }  
  102.     }  
  103.   
  104.     //寫資源文件到XML文件,含中文    
  105.     public static void writePropertiesFileToXML(String filename)  
  106.     {  
  107.         Properties properties = new Properties();  
  108.         try  
  109.         {  
  110.             OutputStream outputStream = new FileOutputStream(filename);  
  111.             properties.setProperty("username""myname");  
  112.             properties.setProperty("password""mypassword");  
  113.             properties.setProperty("chinese""中文");  
  114.             properties.storeToXML(outputStream, "author: [email protected]");  
  115.             outputStream.close();  
  116.         }  
  117.         catch (IOException e)  
  118.         {  
  119.             e.printStackTrace();  
  120.         }  
  121.     }  
  122.   
  123. }  

 

 

運行本程序所需的資源文件,我是放在D盤根目錄,如D:/readfile.properties
1. readfile.properties
username=myname
password=mypassword
chinese=中文
2. writefile.properties
#Fri May 28 22:19:44 CST 2010
password=mypassword
chinese=/u4E2D/u6587
username=myname

    3. readxmlfile.xml
         <?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="password">mypassword</entry>
<entry key="chinese">中文</entry>
<entry key="username">myname</entry>
</properties>
    4. writexmlfile.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>author: [email protected]</comment>
<entry key="password">mypassword</entry>
<entry key="chinese">中文</entry>
<entry key="username">myname</entry>
</properties>
    5. readtxtfile.txt    
           username=myname
           password=mypassword
           chinese=中文
    6. writetxtfile.txt
#Fri May 28 22:25:16 CST 2010
password=mypassword
chinese=/u4E2D/u6587
username=myname
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章