RandomAccessFile 相对其它流多了一个seek() 方法指定指针的偏移量。
1、指定起始位置读取剩余内容
public static void test01() throws IOException {
RandomAccessFile raf = new RandomAccessFile("src/com/xzlf/io/CopyFile.java", "r");
// 随机读取
raf.seek(2);
byte[] flush = new byte[1024];
int len = -1;
while((len=raf.read(flush)) != -1) {
System.out.println(new String(flush,0,len));
}
raf.close();
}
2、指定起始位置和实际大小
public static void test02() throws IOException {
RandomAccessFile raf = new RandomAccessFile("src/com/xzlf/io/CopyFile.java", "r");
// 起始位置
int beginPos = 2+1026;
// 实际大小
int actualSize = 1026;
// 随机操作
raf.seek(beginPos);
// 缓冲容器
byte[] flush = new byte[1024];
int len = -1;
while ((len = raf.read(flush)) != -1) {
if(actualSize > len) {
System.out.println(new String(flush, <