使用RandomAccessFile类-解决readLine乱码问题
import java.io.*;public class RandomFileAccessTest { public static void main(String[] args) throws IOException { long finishedFileSize = 1000 ; File newFile = new File("d:\\result.txt"); //文件大小 long fileSize = newFile.length(); RandomAccessFile raf = new RandomAccessFile(newFile, "r"); raf.seek(finishedFileSize); int offset = 500 ; //无乱码// byte[] b = new byte[1024];// raf.read(b, 0, offset);// System.out.println(new String(b)); //返回1500,当前指针的位置。 1500 = 1000 + 500 = finishedFileSize + offset// System.out.println(raf.getFilePointer()); //乱码 -- 解决了 //在Windows下raf会默认编码成8859_1 while(raf.read()!=-1){ System.out.println(new String(raf.readLine().getBytes("8859_1"),"gbk")); System.out.println("current pointer offset:"+raf.getFilePointer()); } raf.close(); }}
?