合并两个文件

原文:合并两个文件

源代码下载地址:http://www.zuidaima.com/share/1550463699438592.htm

使用java合并两个已存在的文件的内容到一个新的文件去

import java.io.*;
package com.zuidaima.file.util;
/**
*@author www.zuidaima.com
**/
class combinefile{
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//get the path of file
		System.out.print("please enter the path of first file");
		String filebr1=br.readLine();
		System.out.print("please enter the path of second file");
		String filebr2=br.readLine();
		System.out.print("please input the path of combine file");
		String filebr3=br.readLine();
		//transform the real file into object;
		File file1 = new File(filebr1);
		File file2 = new File(filebr2);
		File file3 = new File(filebr3);
		//transform file1 and file2 object into inputstream
		FileInputStream fis1 = new FileInputStream(file1);
		FileInputStream fis2 = new FileInputStream(file2);
		//transform file3 object into outputstream
		FileOutputStream fos = new FileOutputStream(file3);
		//combine two stream into one stream
		InputStream is = new SequenceInputStream(fis1,fis2);
		
		int c;
		while((c=is.read())!=-1)
		{
			fos.write((char)c);
		}

		System.out.println("OK");
		is.close();		
		fos.close();
	}
}

	    			


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值