1. 程式人生 > >這是用InputStream,OutputStream做的兩個TXT檔案合併在另一個檔案裡面

這是用InputStream,OutputStream做的兩個TXT檔案合併在另一個檔案裡面

這是用InputStream,OutputStream做的兩個TXT檔案合併在另一個檔案裡面

a.txt b.txt合併到c.txt

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

/*
 需求:把a.txt與b.txt合併成一個c.txt

*/
public class Hebin {
  public static void main(String[] args) throws Exception {
    merge1();
}
  //需求:把a.txt與b.txt合併成一個c.txt
public static void merge1() throws Exception{ //找到目標檔案 File infile1=new File("E:\\t\\a.txt"); File infile2=new File("E:\\t\\b.txt"); File outfile=new File("E:\\t\\c.txt"); //建立輸入輸出通道 FileInputStream fileInputStream1=new FileInputStream(infile1); FileInputStream fileInputStream2=new
FileInputStream(infile2); FileOutputStream fileOutputStream=new FileOutputStream(outfile); //把輸入快取到集合中,然後再從集合中讀取 ArrayList<FileInputStream> list=new ArrayList<FileInputStream>(); list.add(fileInputStream1); list.add(fileInputStream2); //準備一個緩衝陣列 byte
[] buf=new byte[1024]; int lenght=0; for(int i=0;i<list.size();i++){ FileInputStream fileInputStream=list.get(i); while((lenght=fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf, 0, lenght); } fileInputStream.close(); } fileOutputStream.close(); } }

方法二:## 這是用SequenceInputStream做的 ##

//使用SequenceInputStream合併檔案
  public static void merge2() throws IOException {
      //找到目標檔案
      File infile1=new File("E:\\t\\a.txt");
      File infile2=new File("E:\\t\\b.txt");
      File outfile=new File("E:\\t\\c.txt");
      //建立輸入輸出通道
      FileInputStream fileInputStream1=new FileInputStream(infile1);
      FileInputStream fileInputStream2=new FileInputStream(infile2);
      FileOutputStream  fileOutputStream=new FileOutputStream(outfile);
      //建立一個序列流物件
      SequenceInputStream si=new SequenceInputStream(fileInputStream1, fileInputStream2);
      byte[] buf=new byte[1024];
      int length=0;
      while((length=si.read(buf))!=-1){
          fileOutputStream.write(buf, 0, length);
      }
      si.close();//這裡就把fileInputStream1,fileInputStream2關了
      fileOutputStream.close();
  }

合併三個txt檔案 ,到另外一個

回顧:Vector集合的遍歷方法是elements()返回Enumeration迭代器
SequenceInputStream的構造器有兩個,一個是SequenceInputStream(FileInputStream f1,FileInputStream f2)
一個是
SequenceInputStream(Enumeration e)
所以把幾個流放在Vector中

public static void merge3() throws IOException{
    //找到目標檔案
      File infile1=new File("E:\\t\\a.txt");
      File infile2=new File("E:\\t\\b.txt");
      File infile3=new File("E:\\t\\c.txt");
      File outfile=new File("E:\\t\\d.txt");
      //建立對應的輸入輸出流物件
      FileInputStream fileInputStream1=new FileInputStream(infile1);
      FileInputStream fileInputStream2=new FileInputStream(infile2);
      FileInputStream fileInputStream3=new FileInputStream(infile3);
      FileOutputStream fileOutputStream=new FileOutputStream(outfile);
      //建立序列流物件
      Vector<FileInputStream> vector=new Vector<FileInputStream>();
      vector.add(fileInputStream1);
      vector.add(fileInputStream2);
      vector.add(fileInputStream3);
     //迭代器
      Enumeration<FileInputStream> e=vector.elements();
      SequenceInputStream sequenceInputStream=new SequenceInputStream(e);
      //讀取檔案
      byte[] buf=new byte[1024];
      int lenght=0;
      while((lenght=sequenceInputStream.read(buf))!=-1){
          fileOutputStream.write(buf, 0, lenght);
          }
      sequenceInputStream.close();
      fileOutputStream.close();
      }