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存儲的任何格式文件,究竟有什麼神奇,貓一眼類結構,
原來它繼承了Hashtable並實現了Map接口,這樣大家放心了吧。
print?
- package apistudy;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
-
- public class PropertiesTest
- {
-
- public static void main(String[] args)
- {
-
- String readfile = "d:" + File.separator + "readfile.properties";
- String writefile = "d:" + File.separator + "writefile.properties";
- String readxmlfile = "d:" + File.separator + "readxmlfile.xml";
- String writexmlfile = "d:" + File.separator + "writexmlfile.xml";
- String readtxtfile = "d:" + File.separator + "readtxtfile.txt";
- String writetxtfile = "d:" + File.separator + "writetxtfile.txt";
-
- readPropertiesFile(readfile); //讀取properties文件
- writePropertiesFile(writefile); //寫properties文件
- readPropertiesFileFromXML(readxmlfile); //讀取XML文件
- writePropertiesFileToXML(writexmlfile); //寫XML文件
- readPropertiesFile(readtxtfile); //讀取txt文件
- writePropertiesFile(writetxtfile); //寫txt文件
- }
-
- //讀取資源文件,並處理中文亂碼
- public static void readPropertiesFile(String filename)
- {
- Properties properties = new Properties();
- try
- {
- InputStream inputStream = new FileInputStream(filename);
- properties.load(inputStream);
- inputStream.close(); //關閉流
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- String username = properties.getProperty("username");
- String passsword = properties.getProperty("password");
- String chinese = properties.getProperty("chinese");
- try
- {
- chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 處理中文亂碼
- }
- catch (UnsupportedEncodingException e)
- {
- e.printStackTrace();
- }
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- }
-
- //讀取XML文件,並處理中文亂碼
- public static void readPropertiesFileFromXML(String filename)
- {
- Properties properties = new Properties();
- try
- {
- InputStream inputStream = new FileInputStream(filename);
- properties.loadFromXML(inputStream);
- inputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- String username = properties.getProperty("username");
- String passsword = properties.getProperty("password");
- String chinese = properties.getProperty("chinese"); //XML中的中文不用處理亂碼,正常顯示
- System.out.println(username);
- System.out.println(passsword);
- System.out.println(chinese);
- }
-
- //寫資源文件,含中文
- public static void writePropertiesFile(String filename)
- {
- Properties properties = new Properties();
- try
- {
- OutputStream outputStream = new FileOutputStream(filename);
- properties.setProperty("username", "myname");
- properties.setProperty("password", "mypassword");
- properties.setProperty("chinese", "中文");
- properties.store(outputStream, "author: [email protected]");
- outputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
-
- //寫資源文件到XML文件,含中文
- public static void writePropertiesFileToXML(String filename)
- {
- Properties properties = new Properties();
- try
- {
- OutputStream outputStream = new FileOutputStream(filename);
- properties.setProperty("username", "myname");
- properties.setProperty("password", "mypassword");
- properties.setProperty("chinese", "中文");
- properties.storeToXML(outputStream, "author: [email protected]");
- outputStream.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
-
- }