一 环境配置:Win7+Eclipse3.5+JDK1.5
二 示例代码:
1 主文件代码
2 配置文件内容:
三 调试
将中文的配置文件编码设置为UTF-8,GBK格式,都不行,读取配置文件内容都是乱码,
不是中文的都正常。
解决方式一:
经过调试:配置文件的编码格式必设置为:GBK,在代码中在进行转换操作就行了。
String username =
new String(p.getProperty("username").getBytes("ISO8859-1"),"GBK");
解决方式二:
对配置文件进行GBK编码,正常读取就行了,不需要进行字符集的转换:
下面是编码小工具:
D:
cd D:/bea/jdk150_04/bin
native2ascii -encoding GBK F:/resurl.properties F:/resurl_zh.properties
二 示例代码:
1 主文件代码
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- public class Main {
- public static void main(String[] args) throws Exception{
- readConfigFile();
- }
- private static void readConfigFile() throws IOException {
- InputStream in = new FileInputStream("config.properties");
- Properties p = new Properties();
- p.load(in);
- String username = p.getProperty("username");
- String sex = p.getProperty("sex");
- String age = p.getProperty("age");
- System.out.println("姓名:"+username+",性别:"+sex+",年龄: "+age); /*p.setProperty("username", "陈文");
- p.setProperty("sex", "男");
- p.setProperty("age", "44");
- username = p.getProperty("username");
- sex = p.getProperty("sex");
- age = p.getProperty("age");
- System.out.println("姓名:"+username+",性别:"+sex+",年龄: "+age); */
- }
- }
2 配置文件内容:
- username=陈放
- sex=女
- age=23
三 调试
将中文的配置文件编码设置为UTF-8,GBK格式,都不行,读取配置文件内容都是乱码,
不是中文的都正常。
解决方式一:
经过调试:配置文件的编码格式必设置为:GBK,在代码中在进行转换操作就行了。
String username =
new String(p.getProperty("username").getBytes("ISO8859-1"),"GBK");
解决方式二:
对配置文件进行GBK编码,正常读取就行了,不需要进行字符集的转换:
下面是编码小工具:
D:
cd D:/bea/jdk150_04/bin
native2ascii -encoding GBK F:/resurl.properties F:/resurl_zh.properties
将以上保存为.bat文件格式就行了。这样可解决很多问题,可修改文件中的值(如以上注释了的部分),在进行读取也是正常的,如用第一种方式读取就是乱码。
以上是转载文章
日文环境下,配置文件格式保持为SJIS或SHIFT_JIS(文本软件不同名字不同),然后
String username =
new String(p.getProperty("username").getBytes("ISO8859-1"),"SHIFT_JIS");
或
cd D:/bea/jdk150_04/bin
native2ascii -encoding SHIFT_JIS F:/resurl.properties F:/resurl_jp.properties
最简单的方法是,下载eclipse的插件 properties editor,进行文本转换。