Java学习 序列流SequenceInputStream

将两个文件合并到一个文件中

public static void main(String[] args) throws IOException {
		
		FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
		FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
		//序列流的构造函数 可以接受两个文件输入流
		SequenceInputStream sis = new SequenceInputStream(f1, f2);
		
		FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!=-1){
			fos.write(buf, 0, len);
		}
		fos.close();
		sis.close();
	}

 

但是这个只能添加两个文件

如果想要操作多个需要加入枚举

public static void main(String[] args) throws IOException {
		
		FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
		FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
		FileInputStream f3 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
		
		Vector<FileInputStream> v = new Vector<>();
		v.add(f1);
		v.add(f2);
		v.add(f3);
		Enumeration<FileInputStream> en = v.elements();
	
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\four.txt");
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!=-1){
			fos.write(buf, 0, len);
		}
		fos.close();
		sis.close();
	}
-----------------------下面的更好----------------------------------
public static void main(String[] args) throws IOException {
		
		FileInputStream f1 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\ccc.txt");
		FileInputStream f2 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\suibian.txt");
		FileInputStream f3 = new FileInputStream("C:\\Users\\悠悠华\\Desktop\\test\\yes.txt");
		ArrayList<FileInputStream> a = new ArrayList<>();
		a.add(f1);
		a.add(f2);
		a.add(f3);
		Enumeration<FileInputStream> en = Collections.enumeration(a);
	
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("C:\\Users\\悠悠华\\Desktop\\test\\four.txt");
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!=-1){
			fos.write(buf, 0, len);
		}
		fos.close();
		sis.close();
	}

将文件分割成多个小文件碎片

public class Demo {

	public static void main(String[] args) throws IOException {
		
		File file = new File("C:\\Users\\悠悠华\\Desktop\\test\\abc.bmp");
		split(file);
		
		
	}
	public static void split(File file) throws IOException{
		
		//将文件分割成多个碎片
		FileInputStream fis = new FileInputStream(file);
		
		File dir = new  File("C:\\Users\\悠悠华\\Desktop\\test\\split");
		FileOutputStream fos = null;
		//如果不存在这个文件夹就创建
		if(!dir.exists())
			dir.mkdirs();
		//每次读取1M文件
		byte[] buf = new byte[1024*1024];
		int len = 0;
		int count = 1;
		while((len = fis.read(buf))!=-1){
			
			fos = new FileOutputStream(new File(dir,(count++)+".part"));
			fos.write(buf, 0, len);
		}
		//关闭流
		fos.close();
		fis.close();
		
	}

}

结果:

将分割的文件合并起来

public class Demo {

	public static void main(String[] args) throws IOException {
		

		
		File file = new File("C:\\Users\\悠悠华\\Desktop\\test\\split");
		merge(file);
		
	}
	
	
	public static void merge(File dir) throws IOException{
		ArrayList<FileInputStream> a = new ArrayList<>();
		for(int x = 1; x<=11;x++){
			a.add(new FileInputStream(new File(dir,x+".part")));
		}
		Enumeration<FileInputStream> en = Collections.enumeration(a);
		
		SequenceInputStream sis = new SequenceInputStream(en);
		FileOutputStream fos = new FileOutputStream(new File(dir,"this.bmp"));
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!=-1){
			fos.write(buf, 0, len);
		}
		sis.close();
		fos.close();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值