Java 使用 NIO 进行文件合并输出

2015-08-02 0 296
Java 使用 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();     
  }
}

遇见资源网 java Java 使用 NIO 进行文件合并输出 http://www.ox520.com/9581.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务