复制两个文件中的内容到一个文件中

时间:2021-01-29 21:36:54

复制两个文件中的内容到一个文件中

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.SequenceInputStream;

 

/*

 * 以前的操作:

 *a.txt -- b.txt

 *c.txt -- d.txt

 *

 * 现在想要:

 *a.txt+b.txt -- c.txt

 */

public class SequenceInputStreamDemo {

         publicstatic void main(String[] args) throws IOException {

                   //SequenceInputStream(InputStream s1, InputStream s2)

                   //需求:把ByteArrayStreamDemo.java和DataStreamDemo.java的内容复制到Copy.java中

                   InputStreams1 = new FileInputStream("a.txt");

                   InputStreams2 = new FileInputStream("b.txt");

                   SequenceInputStreamsis = new SequenceInputStream(s1, s2);

                   BufferedOutputStreambos = new BufferedOutputStream(

                                     newFileOutputStream("c.txt"));

 

                   //如何写读写呢,其实很简单,你就按照以前怎么读写,现在还是怎么读写

                   byte[]bys = new byte[1024];

                   intlen = 0;

                   while((len = sis.read(bys)) != -1) {

                            bos.write(bys,0, len);

                   }

 

                   bos.close();

                   sis.close();

         }

}