日期:2014-05-20 浏览次数:20667 次
public class StringReaderTest { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("/home/timing/test.txt","r"); int a; int i=0; do { i++; a = raf.readChar(); System.out.println(i + ":" + (char)a); }while(a != -1); } catch(Exception e) { e.printStackTrace(); } } }
public class StringReaderTest { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("/home/test/test.txt","r"); int a; int i=0; String str; while(( str=raf.readLine())!=null){ String strTemp =new String( str.getBytes("ISO8859-1"),"UTF8"); System.out.println(strTemp); } } catch(Exception e) { e.printStackTrace(); } } }
------解决方案--------------------
可能你文件是 GBK 的
try { RandomAccessFile raf = new RandomAccessFile("/home/timing/test.txt", "r"); byte[] bytes = new byte[2]; long length = raf.length(); for (long i=0; i<length; i++) { bytes[0] = raf.readByte(); if (bytes[0] < 0) { bytes[1] = raf.readByte(); i++; System.out.print(new String(bytes)); } else { System.out.print((char)bytes[0]); } } } catch(Exception e) { e.printStackTrace(); }