public class fenkuaiquzheng{
/**
* @param args
* @throws IOException
*/
//源头
private File src;
//目的地
private String destDir;
//分割后每块的存储路径
private List<String> destpath;
//每块的大小
int blockSize ;
//块数
int size;
public fenkuaiquzheng(String src, String destDir, int blockSize) {
super();
this.src =new File(src);
this.destDir = destDir;
this.blockSize = blockSize;
this.destpath =new ArrayList<>();
info();
}
private void info() {
//总长度
long len =this.src.length();
//每块的大小
this.size=(int) Math.ceil(((len*1.0)/blockSize));
//路径
for(int i=0; i<size; i++) {
this.destpath.add(destDir+"-->"+i+this.src.getName());
}
}
public void spil() throws IOException{
long len =this.src.length();
int begin =0; //分段切割读取
int actualSize = (int) (blockSize>len?len:blockSize);
for(int i=0; i<size; i++) {
begin =i*size;
if(i==size-1) { //最后一块
actualSize =(int) len;
}else {
actualSize =blockSize;
len-=actualSize; //剩余量
}
spil2(i,begin,actualSize);
}
}
public void spil2(int i,int begin,int actualSize) throws IOException {
RandomAccessFile raf =new RandomAccessFile(this.src,"r");
RandomAccessFile raf2 =new RandomAccessFile(this.destpath.get(i),"rw");
raf.seek(begin);
byte [] flush =new byte[1024];
int len;
while((len=raf.read(flush))!=-1) { //获取读取的内容
if(actualSize>len) {
raf2.write(flush,0,len);
actualSize-=len;
}else {
raf2.write(actualSize);
break;
}
}
raf.close();
raf2.close();
}
public void msg(String dest) throws IOException { //文件的合并
//输出流
OutputStream os =new BufferedOutputStream(new FileOutputStream(dest,true)); //读的时候从源头读
for(int i=0;i<destpath.size(); i++) {
//输入流
InputStream is =new BufferedInputStream (new FileInputStream(destpath.get(i)));
byte[] flush=new byte[1024];
int len;
while((len=is.read(flush))!=-1) {
os.write(flush,0,len);
}
os.flush();
is.close();
}
os.close();
}
public static void main(String[] args) throws IOException {
/**
try(BufferedReader br = new BufferedReader(new InputStreamReader(new
URL("http://www.baidu.com").openStream(),"UTF-8"));){ String msg;
while((msg=br.readLine())!=null) { System.out.print(msg); }
}catch(IOException e) { System.out.println("dd"); }
PrintStream p =System.out;
p.print(145);
//打印流
p =new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")));
p.print(545);
p.flush();
p.close();
//重定向输出端
System.setOut(p);
System.out.println("打印流"); //直接输出到文件中
*/
}
}
io流分块取整
猜你喜欢
转载自blog.csdn.net/richpersion/article/details/105125703
今日推荐
周排行