请指教一下哪里逻辑不对,或者汉字编码有问题,数字和字母可以正常输出,但汉字会乱码,初学java不太懂,代码也很乱,请见谅
import java.io.*;
public class E4_1 {
public static void main(String args[]) {
// File file = new File("E:\\Java\\eclipse-workspace\\Test10\\src\\E","E4_1.java");
File file = new File("E4_1test.txt");
// int[] b = {1,2,3,4,5};
String str = "上海自来水321";
try {
RandomAccessFile raf = new RandomAccessFile(file,"rw");//按字节顺序读取
raf.writeUTF(str);
// for(int i = 0;i<b.length;i++) {
// raf.writeInt(b[i]);
// }
long length = file.length();
System.out.println(length);
// byte b = new byte[length]
while(length>=0) {
length=length-1;
raf.seek(length);
int c = raf.readByte();
if(c<=255&&c>=0) {
System.out.print((char)c);
}
else {
length=length-1;
raf.seek(length);
byte[] cc = new byte[2];
raf.readFully(cc);
byte t = cc[1];
cc[1] = cc[0];
cc[0] = t;
// String s = new String(cc);
// byte tom[] = s.getBytes("iso-8859-1");
// String p = new String(tom);
// System.out.print(p);
System.out.println(new String(cc));
}
}
// raf.close();
}
catch(IOException e) {}
}
}