合并(序列)流

/*
 * 合并(序列)流
 */
package Test2;

import java.io.*;

public class Demo23_11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Demo23_11 d = new Demo23_11();
	}

	public Demo23_11()
	{
		//声明两个文件读入流
		FileInputStream in1 = null,in2 = null;
		//声明一个合并流
		SequenceInputStream s = null;
		FileOutputStream out = null;

		//1.txt和2.txt必须事先存在并已经写入

		File input1 = new File("D:" + File.separator + "1.txt");
		File input2 = new File("D:" + File.separator + "2.txt");
		
		File output = new File("D:" + File.separator + "12.txt");
		
		try {
			in1 = new FileInputStream(input1);
			
			in2 = new FileInputStream(input2);
			
			//将两个输入流合并为一个输出流
			s = new SequenceInputStream(in1, in2);
			out = new FileOutputStream(output);
			
			//read在读到流的结尾处返回-1
			int c;
			while((c = s.read()) != -1)
			{
				out.write(c);
			}
			
			System.out.println("ok");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//如果说这四个还没有读取完毕也要关闭,防止异常
			
			try {
				in1.close();
				in2.close();
				s.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			if(in1 != null)
//				try {
//					in1.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			
//			if(in2 != null)
//				try {
//					in2.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			
//			if(s != null)
//				try {
//					s.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			
//			if(out != null)
//				try {
//					out.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值