目录

java将u开头的是什么编码_十六进制u开头字符串的转码

目录

java将u开头的是什么编码_十六进制“\u”开头字符串的转码

有时在JS或JAVA属性文件中,常看到“\u”开头的中文字符串,不能知道其到底是什么字符。现在提供一个转码的方法,将其变成可识别的汉字。

代码如下:

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.ByteArrayOutputStream;

import java.io.FileReader;

import java.io.IOException;

import org.apache.commons.lang.StringEscapeUtils;

public class StringTest {

public String fileAsString(String fileName){

BufferedReader br = null;

ByteArrayOutputStream bos = null;

try {

br = new BufferedReader(new FileReader(fileName));

bos = new ByteArrayOutputStream();

String line = null;

while ((line = br.readLine()) != null) {

String line2 = StringEscapeUtils.unescapeJava(line);

bos.write(line2.getBytes());

bos.write("\r\n".getBytes());

}

String result = new String(bos.toByteArray());

System.out.println(“result:” + result);

return result;

} catch (Exception e) {

e.printStackTrace();

} finally {

if(br != null){

try {

br.close();

} catch (Exception e2) {

// TODO: handle exception

}

}

}

return null;

}

public static void main(String[] args) {

String file = “d:\test.js”;

StringTest st = new StringTest();

st.fileAsString(file);

}

}

主要手段是通过Apache的commons.lang包(类:org.apache.commons.lang.StringEscapeUtils)来进行转码。

要解码的文件内容形如:

message:\"\u4EB2\u7231\u7684\uFF0C\u4F60\u6162\u6162\u98DE\uFF0C\u5C0F\u5FC3\u524D\u9762\u5E26\u523A\u7684\u73AB\u7470…\",解码结果:

message:"亲爱的,你慢慢飞,小心前面带刺的玫瑰…",