Java实现多个文档合并输出到一个文档

本文实例为大家分享了Java实现多个文档合并输出到一个文档的具体代码,供大家参考,具体内容如下

方法:Java NIO

package First;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class Test {

  
  public static void main(String params[]) throws Exception {
    String[] iF = new String[] {"E:/test1.txt", "E:/test2.txt", "E:/test3.txt", "E:/test4.txt" };
    String oF = "E:/out.txt";
    
    FileOutputStream output = new FileOutputStream(new File(oF));
    WritableByteChannel targetChannel = output.getChannel();
    
    for(int i =0; i<iF.length; i++){
      FileInputStream input = new FileInputStream(iF[i]);
      FileChannel inputChannel = input.getChannel();
      
      inputChannel.transferTo(0, inputChannel.size(), targetChannel);
      
      inputChannel.close();
      input.close();
    }
    targetChannel.close();
    output.close();
    System.out.println("All jobs done...");
    
  }


  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: