RandomAccessFile 读写文件时,不管文件中保存的数据编码格式是什么 使用 RandomAccessFile对象方法的 readLine() 都会将编码格式转换成 ISO-8859-1 所以 输出显示是还要在进行一次转码
字符串转码操作
str = new String(str.getBytes("ISO-8859-1"),"UTF-8");
例子:
public class RandomAccessFileTest{
public static void main(String[] args) throws Exception {
RandomAccessFile ra = new RandomAccessFile("test.txt", "rw");
ra.seek(0);
ra.write("你在说什么".getBytes());
ra.seek(0);
System.out.println(new String(ra.readLine().getBytes("ISO-8859-1"),"utf-8"));//需要重新转码才能正常显示
ra.close();
}
}