java读取含有unicode编码的文件内容,并转换成汉字?
有一个asci编码的文件,里面的内容是\u8521\u7eaa,这是2个汉字,问题是不知道通过什么方法能够从文件中读出来,并且转换为汉字,希望哪位大拿给个代码....
有一个asci编码的文件,里面的内容是\u8521\u7eaa,这是2个汉字,问题是不知道通过什么方法能够从文件中读出来,并且转换为汉字,希望哪位大拿给个代码.
展开
3个回答
展开全部
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容(unicode会自动转换为中文的)
};
备注:unicode不需要转换的,直接输出即可,会自动变成中文,如:
System.out.println("\u0061\u0062\u6c49\u5b57");
结果就是:ab汉字。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容(unicode会自动转换为中文的)
};
备注:unicode不需要转换的,直接输出即可,会自动变成中文,如:
System.out.println("\u0061\u0062\u6c49\u5b57");
结果就是:ab汉字。
展开全部
public static void main(String[] args) throws IOException {
String path = "E:/a.txt";
File file = null;
BufferedReader br = null;
file = new File(path);
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));
StringBuilder sb = new StringBuilder();
String length = "";
while ((length = br.readLine()) != null) {
sb.append(length);
}
System.out.println(ascii2Native(sb.toString()));
}
//unicode转为本地
public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf("\\u");
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf("\\u", begin);
}
sb.append(str.substring(begin));
return sb.toString();
}
private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException(
"Ascii string of a native character must be 6 character.");
}
if (!"\\u".equals(str.substring(0, 2))) {
throw new IllegalArgumentException(
"Ascii string of a native character must start with \"\\u\".");
}
String tmp = str.substring(2, 4);
int code = Integer.parseInt(tmp, 16) << 8;
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16);
return (char) code;
}
}
String path = "E:/a.txt";
File file = null;
BufferedReader br = null;
file = new File(path);
br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));
StringBuilder sb = new StringBuilder();
String length = "";
while ((length = br.readLine()) != null) {
sb.append(length);
}
System.out.println(ascii2Native(sb.toString()));
}
//unicode转为本地
public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf("\\u");
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf("\\u", begin);
}
sb.append(str.substring(begin));
return sb.toString();
}
private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException(
"Ascii string of a native character must be 6 character.");
}
if (!"\\u".equals(str.substring(0, 2))) {
throw new IllegalArgumentException(
"Ascii string of a native character must start with \"\\u\".");
}
String tmp = str.substring(2, 4);
int code = Integer.parseInt(tmp, 16) << 8;
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16);
return (char) code;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
逐个字符读,遇到\u取后面4个字符,转换成byte[] byte4s、再转换成new String(byte4s, "utf-8");第二个参数随你文件中真正的字符集而改变。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询