初用properties,读取java properties文件的时候如果value是中文,会出现读取乱码的问题。给定country.properties文件如下:
China=中国
USA=美国
Japan=日本
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/country.properties");
properties.load(inputStream );
System.out.println(properties.getProperty("China"));
上面的程序执行后的结果会出现中文乱码,因为字节流是无法读取中文的,所以采取reader把inputStream转换成reader用字符流来读取中文。代码如下:
Properties properties = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/country.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
properties.load(bf);
System.out.println(properties.getProperty("China"));