使用 IO 流,将多个文件合并成一个文件

Java 代码:

 

import java.io.*;
import java.util.*;
class SequenceInputStreamDemo
{
	public static void main (String[] args) throws IOException
	{
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		
		v.add(new FileInputStream("1.txt"));
		v.add(new FileInputStream("2.txt"));
		v.add(new FileInputStream("3.txt"));
		
		Enumeration<FileInputStream> en = v.elements();
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("4.txt");
		
		byte[] buf = new byte[1024];
		
		int len = 0;
		
		while((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
		}
		
		fos.close();
		sis.close();
	}
}


 

你可能感兴趣的:(使用 IO 流,将多个文件合并成一个文件)