用java nio合并两个小文件的方法

来源:互联网 发布:作弊大师软件下载 编辑:程序博客网 时间:2024/05/21 07:12
package com.github.elizabetht.controller;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class FileGathering {    public static void main(String[] args) throws IOException {        long start = System.currentTimeMillis();        File fileIn1 = new File("C:\\from1.txt");        File fileIn2 = new File("C:\\from2.txt");        File fileOut = new File("C:\\to.txt");        FileInputStream fin1 = new FileInputStream(fileIn1);        FileInputStream fin2 = new FileInputStream(fileIn2);        FileOutputStream fout = new FileOutputStream(fileOut);        FileChannel fcIn1 = fin1.getChannel();        FileChannel fcIn2 = fin2.getChannel();        ByteBuffer[] bufferArray = new ByteBuffer[2];        bufferArray[0] = ByteBuffer.allocate(1024);        bufferArray[1] = ByteBuffer.allocate(1024);        fcIn1.read(bufferArray[0]);        fcIn2.read(bufferArray[1]);        bufferArray[0].flip();        bufferArray[1].flip();        FileChannel fcOut = fout.getChannel();        fcOut.write(bufferArray);        long end = System.currentTimeMillis();        System.out.println("time used" + (end - start));    }}
0 0
原创粉丝点击