随机流的代码1
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandTest03 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile(new File("src/cn/jd/io/Copy.java"), "r");
//起始位置
int beginPos=2;
//实际大小
int actualSize=1026;//我要读取1026个字节
//随机读取
raf.seek(beginPos); //从第二个索引开始读取,记住索引是从0开始的
//读取 读取方式和字节流的读取方式是一样的
byte[] flush=new byte[1024];
int len=-1;
while((len=raf.read(flush))!=-1) {
if(len<=actualSize) {//获取本次读取的所有内容
System.out.println(new String(flush,0,len));
actualSize-=len;//还剩余多少字节需要下次读取
}else {
System.out.println(new String(flush,0,actualSize));
break;
}
}
raf.close();
}
}