java _io_随机读取读入流RandomAccessFile
作者:互联网
- 随机读取和写入流RandomAccessFile
- 支持读取和写入随机访问文件
-
RandomAccessFile raf=new RandomAccessFile(文件对象,读写模式);r只读,rw读和写
private File f; //目的地 private String dir; //所有分割后的文件存储路径 private List<String> list; //每块大小 private int blockSize; //块数 private int size;
public n(File f,String dir,int blockSize)
{
this.f=f;
this.dir=dir;
this.blockSize=blockSize;
this.list=new ArrayList<String>;
}//初始化
private void init()
{
//总长度
long len=this.f.length();
//块数
this.size=(int)Math.ceil(len*1.0/blockSize);
//路径:
for(int i=0;i<size;i++)
{
this.list.add(this.dir+i+this.f.getName());
}}
//分割:计算每一块的起始位置及大小
public void split()
{}
public void t3(int i,int first,int real) throws IOException
{
RandomAccessFile raf=new RandomAccessFile(this.f,"r");
RandomAccessFile raf2=new RandomAccessFile(this.list.get(i),"rw");raf.seek(first);//从第二个位置开始 byte[] flush=new byte[1024]; int len=-1; while((len=raf.read(flush))!=-1) { if(real>1024) { raf2.write(flush,0,len); real-=1024; }else { raf2.write(flush,0,real); break; //读取指定大小,读完退出 } raf2.close(); raf.close();
}
}
public static void main(String[]args)
{}
}
标签:raf,java,int,blockSize,private,RandomAccessFile,io,new 来源: https://blog.51cto.com/14437184/2425957