Properties讀寫資源文件

jopen 10年前發布 | 14K 次閱讀 Java開發 Properties

Java中讀寫資源文件最重要的類是Properties,功能大致如下:

1. 讀寫Properties文件

2. 讀寫XML文件

3. 不僅可以讀寫上述兩類文件,還可以讀寫其它格式文件如txt等,只要符合key=value格式即可.


注意:資源文件中含有中文時的處理方法 

1. 將中文字符通過工作轉成utf8編碼,可以通過Java自帶的nativetoascii或Eclipse中的屬性編輯器。

2. 直接調用 new String(youChineseString.getBytes("ISO-8859-1"), "GBK");或者new String(youChineseString.getBytes("ISO-8859-1"), "UTF-8");


附:WEB程序中加載資源文件的方法

Properties prop = null; 

1. prop = Thread.currentThread().getContextClassLoader().getResourceAsStream("filename");
2. prop = this.getClass().getClassLoader().getResourceAsStream("filename");

下面是文件操作的代碼:

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;
import com.liuyazhuang.utl.StringUtils;

public class PropertiesUtil {

// 讀取資源文件,并處理中文亂碼
public static String readPropertiesFile(String filename,String key) {
    if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;
    Properties properties = new Properties();
    try {
        InputStream inputStream = new FileInputStream(filename);
        properties.load(inputStream);
        inputStream.close(); // 關閉流
    } catch (IOException e) {
        e.printStackTrace();
    }
    String value = properties.getProperty(key);
    try {
        value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); // 處理中文亂碼
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return value;
}

// 讀取XML文件,并處理中文亂碼
public static String readPropertiesFileFromXML(String filename,String key) {
    if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())) return null;
    Properties properties = new Properties();
    try {
        InputStream inputStream = new FileInputStream(filename);
        properties.loadFromXML(inputStream);
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties.getProperty(key);
}

// 寫資源文件,含中文
public static void writePropertiesFile(String filename,String key,String value) {
    if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;
    Properties properties = new Properties();
    try {
        OutputStream outputStream = new FileOutputStream(filename);
        properties.setProperty(key, value);
        properties.store(outputStream, null);
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// 寫資源文件到XML文件,含中文
public static void writePropertiesFileToXML(String filename,String key,String value) {
    if(StringUtils.isEmpty(filename.trim())||StringUtils.isEmpty(key.trim())||StringUtils.isEmpty(value.trim())) return;
    Properties properties = new Properties();
    try {
        OutputStream outputStream = new FileOutputStream(filename);
        properties.setProperty(key, value);
        properties.storeToXML(outputStream, null);
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//main方法測試
public static void main(String[] args) {
    writePropertiesFile("d:/test.properties", "hello", "world");
    System.out.println(readPropertiesFile("d:/test.properties", "hello"));
}

}</pre></span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!