Java 读Properties

import java.io.*;
import java.util.Properties;

public class Study {
    public static void main(String[] args) throws Exception {
        String strUserDir = System.getProperty("user.dir");
        System.out.println(strUserDir);
        FileInputStream fis = new FileInputStream(strUserDir + "\\my.properties");
        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
        Properties property = new Properties();
        property.load(isr);
        fis.close();
        String strTest = property.getProperty("test");
        System.out.println(strTest);
        property.list(System.out);
    }
}

properties文件内容:

test=123
foo=abcd我d

注意:properties文件使用notepad++保存为UTF-8时有两个选择项:UTF-8和UTF-8 with-out BOM
使用UTF-8 with-out BOM来保存,不然读不出数据。

原因是因为微软的windows对UTF-8会加多一个BOM字符,在其它操作系统不会认识这个BOM字符。

posted @ 2014-03-25 15:11  横渡  阅读(234)  评论(0编辑  收藏  举报