IO流 随机读取和写入流 RandomAccessFile(加入文件的合并(SequenceInputStream))

package io03;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**

  • 面对对象思想封装 分割
  • 随机读取和写入流 RandomAccessFile
  • 加入文件的合并(SequenceInputStream)
  • @author

*/
public class TestRandom3 {
//源头
private File src;
//目的地(文件夹)
private String destDir;
//所有分割后的文件存储路径
private List destPath;
//每块大小
private int blockSize;
//块数:多少块
private int size;

public TestRandom3(String srcPath,String destDir,int blockSize) {
	this.src = new File(srcPath);
	this.blockSize = blockSize;
	this.destPath = new ArrayList<String>();
	
	//初始化
	init();
	
}

//初始化
private void init() {
	//总长度
	long len = this.src.length();
	
	//每块大小

// int blockSize = 1024;

	//多少块
	this.size = (int)Math.ceil(len*1.0/blockSize);
		
	//路径
	for(int i=0; i<size; i++) {
		this.destPath.add(this.destDir+"/"+i+"-->"+this.src.getName());
	}
}

/**
 * 分割
 * 1、计算每一块的起始位置及大小
 * 2、分割
 * @throws IOException 
 */
public void split() throws IOException {	//分割

// File src = new File(“E:/mycode/TestIO/src/1.jpg”);

	//总长度
	long len = src.length();
	
	//每块大小

// int blockLength = 1024;

	//多少块
	int size = (int)Math.ceil(len*1.0/blockSize);
	
	//起始位置 实际大小
	int begin = 0;
	int actualSize = (int) (blockSize>len?len:blockSize);
	for(int i = 0; i<size; i++) {
		begin = i*blockSize;
		if(i == size-1) {//最后一块
			actualSize = (int) len;
		}else {
			actualSize = blockSize;
			len -= actualSize;//剩余量
		}
		System.out.println(i+"-->"+begin+"-->"+actualSize);
		test2(i, begin, actualSize);
	}
}

//分块思想:起始 实际大小
/**
 * 指定第i块的起始位置和实际长度 
 * @param i
 * @param begin
 * @param actualSize
 * @throws IOException
 */
private void test2(int i,int begin,int actualSize) throws IOException {
	RandomAccessFile raf = new RandomAccessFile(new File("this.src"), "r");
	RandomAccessFile raf2 = new RandomAccessFile(new File(this.destPath.get(i)), "rw");
	//起始位置

// int begin = 2;

	//实际大小
	int size= actualSize;
	
	//随机读取
	raf.seek(begin);
	
	//读取
	byte[] temp = new byte[1024];
	int len  = -1;
	while((len = raf.read(temp)) != -1) {
		
		if(size > len) {
			//获取本次读取的所有内容
			raf2.write(temp, 0, len);
			size -= len;
		}else {
			raf2.write(temp, 0, actualSize);
			break;
		}
	}	
	raf2.close();
	raf.close();
}

/**
 * 
 * 文件的合并
 * @throws IOException 
 */
public void merge(String destPaths) throws IOException {
	//输出流
	OutputStream os = new BufferedOutputStream(new FileOutputStream(destPaths,true));
	
	Vector<InputStream> vi = new Vector<InputStream>();
	SequenceInputStream sis = null;
	
	//输入流
	for(int i=0; i<destPath.size(); i++) {
		vi.add(new BufferedInputStream(new FileInputStream(destPath.get(i))));
	}
	sis = new SequenceInputStream(vi.elements());//序列流
	//拷贝
	byte[] temp = new byte[1024];
	int len = -1;
	while((len = sis.read(temp)) != -1) {
		os.write(temp,0,len);
	}
	os.flush();
	sis.close();
	os.close();
} 

public static void main(String[] args) throws IOException {
	TestRandom3 tr = new TestRandom3("1.jpg", "dest", 1024*10);
	tr.split();
	tr.merge("2.jpg");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值