Java直接读取properties文件时,中文会是乱码,这时可以用Java/jdk1.x.x/bin/native2ascii.exe工具。
例如,把f1.properties转换成f2.properties。
在cmd的当前目录下输入
- native2ascii -encoding gbk f1.properties f2.properties
转换前f1.properties:
- #编号=描述
- cn=中国
转换后f2.properties:
- #/u7f16/u53f7=/u63cf/u8ff0
- cn=/u4e2d/u56fd
这样Java程序就可以正确识别中文了!
- Properties prop=new Properties();
- InputStream inputStream=new FileInputStream("E://f2.properties");
- prop.load(inputStream);
- String title=prop.getProperty("cn");
- System.out.println(title);
Properties prop=new Properties();
InputStream inputStream=new FileInputStream("E://f2.properties");
prop.load(inputStream);
String title=prop.getProperty("cn");
System.out.println(title);
转换前运行结果:
- ???ú
转换后运行结果:
- 中国