`

RandomAccessFile类 乱码问题的解决

 
阅读更多

//用RandomAccessFile读取出来乱码的解决

String path = "d:/test.txt";

   RandomAccessFile raf = null;
   String line = null;
   try {

    raf = new RandomAccessFile(path, "rw");

    while ((line = raf.readLine()) != null) {
     line = new String(line.getBytes("8859_1"), "gb2312");//转变编码格式
     System.out.println(line);
    }

   } catch (Exception e) {

    e.printStackTrace();
   } finally {

    try {
     raf.close();
    } catch (IOException e) {

     e.printStackTrace();
    }
   }

 

 

//用RandomAccessFile写入文件乱码的解决

raf.write(str.getBytes());

分享到:
评论
2 楼 181054867 2011-11-16  
这个乱码是因为读了半个字节出来,这个问题难以解决,我现在也找不到方法
1 楼 yuzhu223 2011-02-14  
这段代码没有解决问题啊,我运行你的代码后发现输出的依旧是乱码
源文件的内容为:
nihao
早上好!

用你的方法输出的依旧是乱码

相关推荐

Global site tag (gtag.js) - Google Analytics