JAVA Properties中文乱码问题

如果properties文件中存有中文字符,用以下代码读取出来中文会出现乱码:

public static String getString(String Name, String defaultValue) {
        String szRet = defaultValue;
        Properties prop = new Properties();
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(FILE_PATH);
            prop.load(fs);
            szRet = prop.getProperty(Name, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fs != null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return szRet;
    }

解决方法:
将FileInputStream改为InputStreamReader

public static String getString(String Name, String defaultValue) {
        String szRet = defaultValue;
        Properties prop = new Properties();
        InputStreamReader reader = null;
        try {
            reader =  new InputStreamReader(new FileInputStream(FILE_PATH),"utf-8");
            prop.load(reader);
            szRet = prop.getProperty(Name, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return szRet;
    }

你可能感兴趣的:(java)