[size=medium]在开发过程中,为了使开发出来的代码更健壮、更灵活,我们常常把一些常变的变量写
入到配置文件中。而在这些配置文件中,properties文件是使用的比较多的,它不仅配置起
来简单(一个key=value形式即可),而且在对文件的操作中也比较方便。
由于字符的编码问题以及properties文件在国际化的使用过程中,我们常常需要将文件
内容转换为unicode码。以免从文件里取出的内容为乱码。
解决上面问题,我们一般采用一些如 properties editor 形式的插件。现在我写了一段
类似properties editor的转码程序(转换为unicode码)。大致功能是:从一个properties文件里逐行读出一字符串(有可能是注释,也有可能是 'key=value' 形式的属性配置)。然后将属性配置行的value转换为unicode码,转换完后写入到另一个文件里。
现在的问题是,上面功能能完成,即可以把properties文件转换成一个内容为unicode的
properties文件(后面称unicode文件)。但是,在从unicode文件读文件配置内容时候,就会报错,或根本就不能通过Properties.load(in)的形式加载文件。希望您能花点时间,帮我解决这个问题。
也许我描述的不太清楚,现在我贴出部分代码,您先看看,知道我的大概意思,然后把整
个工程下载到你本地,你帮我调试一下。先说明一下,对这个问题,我研究了好几天,实在找
不到解决问题的方法,才贴出的。请大家别责怪我不思考就贴出问题来。
package com.tja.code;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Queue;import com.tja.util.StringUtil;
import com.tja.util.file.CREATE_METHOD;
import com.tja.util.file.FileUtil;/**
*
*/
public class UnicodeUtil {
/**
public static void propFile2Unicode(String fileName) throws FileNotFoundException, IOException {
//Assert.notNull(fileName);
propFile2Unicode(new File(fileName),null);
}
/**
/**
/**
/**
/**
/**
/**
public static void main(String[] args) throws FileNotFoundException, IOException {
String str = "D:/bb.properties";
String str1 = "D:/cc.properties";
String ss = "项目额度";
UnicodeUtil.propFile2Unicode(new File(str),str1);
String g = UnicodeUtil.GBK2Unicode(ss);
System.out.println(g);
InputStream in = new FileInputStream(new File(str1));
Properties p = new Properties();
p.load(in);
System.out.println(p.getProperty("FMS.loader.project"));
System.out.println(p.getProperty("FMS.chinese"));
}
}
请大家有时间帮忙看看。哦运行报的错误如下:
Exception in thread "main" java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
at java.util.Properties.loadConvert(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.tja.code.UnicodeUtil.main(UnicodeUtil.java:175)[/size]
问题补充
所以问题就是出在你读取原始properties文件上了。
这部分调试下吧,确保文件中的内容读出来不乱码就好了,这应该不是什么难的问题吧。
另外如果用readLine读的话,别丢掉了换行符。
load的时候指定编码
好像不能使用你写的程序来转码吧,jdk中就提供了一个native2ascii.exe可以将中文转换成unicode码,原理和你写的不一样吧。
BufferedReader 看你读文件的时候没有指定charset,这时候得到的string本身可能就是乱的。
参考下下面的代码吧:
[code="java"]
public static void main(String[] argv) {
//确保str的获取是基于正确的文件读取,编码不能有误。
String str = "a.value=中国人\n\r" + "b.value=美国人";
//简单的转换处理,ascii码<=126的粗略地默认为不用转换(包含了=号,换行,空格等)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch <= 126) {
sb.append(ch);
} else {
sb.append("\\u");
sb.append(Integer.toString(ch, 16));
}
}
Properties prop = new Properties();
File pFile = new File("d:/test.properties");
try {
//写入文件,指定utf-8编码
FileOutputStream fout = new FileOutputStream(pFile);
fout.write(sb.toString().getBytes("utf-8"));
fout.close();
//从文件中读取流,由于已经转化成\\uxx 形式的内容,因此load时候不用在意编码
FileInputStream fin = new FileInputStream(pFile);
prop.load(fin);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(prop.get("a.value"));
System.out.println(prop.get("b.value"));
}
[/code]