Java 使用 NIO 進行文件合并輸出

n2fy 9年前發布 | 813 次閱讀 Java

 
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();     

} } </pre>

 本文由用戶 n2fy 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!