RandomAccessFile 文件读写中文乱码解决方案!

时间:2023-03-09 05:34:24
RandomAccessFile 文件读写中文乱码解决方案!

RandomAccessFile 读写文件时,不管文件中保存的数据编码格式是什么   使用 RandomAccessFile对象方法的 readLine() 都会将编码格式转换成 ISO-8859-1 所以 输出显示是还要在进行一次转码

例子:

package fileReadAndWrite;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile; /**
* 测试文件读写
* @author laobiao
*
*/
public class bigFileRW { public static void main(String[] args) throws Exception {
RandomAccessFile ra = new RandomAccessFile("test.txt", "rw");
ra.seek(0);
ra.write("a bcd你好啊的撒法".getBytes());
ra.seek(0);
System.out.println(new String(ra.readLine().getBytes("ISO-8859-1"),"utf-8"));//需要重新转码才能正常显示 ra.close(); } }