欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java 使用 NIO 进行文件合并输出,nio文件合并,代码很酷,NIO的思路,

来源: javaer 分享于  点击 3575 次 点评:93

Java 使用 NIO 进行文件合并输出,nio文件合并,代码很酷,NIO的思路,


代码很酷,NIO的思路,性能好。

import java.io.*;import java.nio.*;import java.nio.channels.*;public class NIOCat {  public static void main(String[] args) throws IOException {    if (args.length < 2) {      System.err.println("Usage: java NIOCat inFile1 inFile2... outFile");      return;    }    ByteBuffer[] buffers = new ByteBuffer[args.length-1];    for (int i = 0; i < args.length-1; i++) {      RandomAccessFile raf = new RandomAccessFile(args[i], "r");      FileChannel channel = raf.getChannel();      buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());    }    FileOutputStream outFile = new FileOutputStream(args[args.length-1]);    FileChannel out = outFile.getChannel();    out.write(buffers);    out.close();       }}//该片段来自于http://byrx.net
相关栏目:

用户点评