类名:Test.java ,作用:读取一个文本文件中的繁体字,然后转为unicode.aa.txt中的内容为:舉辦!当我把类的Text file encoding设置gbk时,一切都能正常显示,可是我现在要把Text file encoding设为utf-8,此时
文字变成了乱码,unicode也不是文本中文字对应的编码了,请问怎么解决.我试着在程序中转码也不行,现在必须用utf-8.
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;public class Test {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} return str;
} private static String convert(String str) {
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c > 255) {
sb.append("\\u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(c);
} }
return (new String(sb));
} public static void main(String[] args) {
Test t = new Test();
String str = t.readTxt(0, 5);
System.out.println(str);
System.out.println(convert(str));
}
}上面的程序如果将Test.java的Text file encoding设置utf-8时,就会乱码,请帮忙解决
文字变成了乱码,unicode也不是文本中文字对应的编码了,请问怎么解决.我试着在程序中转码也不行,现在必须用utf-8.
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;public class Test {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} return str;
} private static String convert(String str) {
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c > 255) {
sb.append("\\u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(c);
} }
return (new String(sb));
} public static void main(String[] args) {
Test t = new Test();
String str = t.readTxt(0, 5);
System.out.println(str);
System.out.println(convert(str));
}
}上面的程序如果将Test.java的Text file encoding设置utf-8时,就会乱码,请帮忙解决
\u8209\u8fa6!可能是你电脑的语言什么的问题噢
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;public class TestEncoding {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} return str;
} public static void main(String[] args) {
TestEncoding t = new TestEncoding();
String str = t.readTxt(0, 7);
try {
System.out.println(new String(str.getBytes(),"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}把源文件的编码另存为utf-8就不会乱码!utf-8一个汉字占三个字节。