package file;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class TestRandomAccessFile {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("道德经.txt", "r");
System.out.println("当前的RandomAccessFile的初始指针位置是:" + raf.getFilePointer());
// 设置文件指针的偏移量
raf.seek(100);
System.out.println("设置偏移量之后的文件位置是:" + raf.getFilePointer());
// 尝试读取文件
byte[] bytes = new byte[1024];
int len;
while ((len = raf.read(bytes)) != -1) {
// 处理编码格式
byte[] ecode = new String(bytes, 0 ,len, "utf-8").getBytes();
String str = new String(ecode);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}