java中如何在中文字符和unicode编码之间进行转换?
发布网友
发布时间:2022-04-21 16:24
我来回答
共1个回答
热心网友
时间:2023-06-21 19:22
利用JDK中自带的native2ascii工具就很好
如果是在项目中,可以直接拷贝%JDK%/bin下native2ascii.exe至项目根目录,然后类中这样调用即可
中文转UNICODE(srcFileName为中文文件路径)
StringBuffer
tempSb
=
new
StringBuffer();
Process
p
=
Runtime.getRuntime().exec(
"native2ascii
"+srcFileName);
InputStreamReader
child_in
=
new
InputStreamReader(p
.getInputStream());
int
c;
while
((c
=
child_in.read())
!=
-1)
{
tempSb.append((char)
c);
}
System.out.println(tempSb);
UNICODE转中文(srcFileName为UNICODE文件路径)
StringBuffer
tempSb
=
new
StringBuffer();
Process
p
=
Runtime.getRuntime().exec(
"native2ascii
-reverse
"+srcFileName);
InputStreamReader
child_in
=
new
InputStreamReader(p
.getInputStream());
int
c;
while
((c
=
child_in.read())
!=
-1)
{
tempSb.append((char)
c);
}
System.out.println(tempSb);