码迷,mamicode.com
首页 > 其他好文 > 详细

合并两个文件

时间:2015-03-13 10:54:18      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:文件合并   file   java   

原文:合并两个文件

源代码下载地址: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();
	}
}

	    			


合并两个文件

标签:文件合并   file   java   

原文地址:http://blog.csdn.net/springmvc_springdata/article/details/44237987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!