RandomAccessFile 文件读写中文乱码解决

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();    
    }
}

原址: https://www.cnblogs.com/laobiao/p/5585862.html

猜你喜欢

转载自blog.csdn.net/l1336037686/article/details/80304742
今日推荐