使用FileChannel.transferFrom和while()时,哪种复制数据在Java中更快?

时间:2013-11-25 06:08:56

标签: java nio

以下Java代码以两种方式复制文件哪个更高效?希望能够解释原因,谢谢。 顺便说一句,我测试了相同的文件,大小约为300兆字节。第一种方法的速度和第二种方法一样快。但经过多次测试,结果却不同。 我的测试结果之一如下:

  • NO.1:使用transferFrom花了3396ms
  • NO.1:使用时间为3334ms
  • NO.2:使用transferFrom花了4239ms
  • NO.2:使用时间为3225毫秒
  • NO.3:使用transferFrom花了3906ms
  • NO.3:使用时间为4153ms
  • NO.4:使用transferFrom花了3100ms
  • NO.4:使用时间为3174毫秒
  • NO.5:使用transferFrom花了3156ms
  • NO.5:使用时间为3180毫秒

代码:

public void copyData(String inPath, String outPath1, String outPath2) {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    FileInputStream fin = null;
    FileOutputStream fout = null;
    File out1File = null;
    File out2File = null;
    int bufferLen = 20480 * 2 * 1024;
    long runTime;
    ByteBuffer bb = ByteBuffer.allocateDirect(bufferLen);

    for (int i = 0; i < 5; i++) {
        try {
            fin = new FileInputStream(new File(inPath));
            out1File = new File(outPath1);
            fout = new FileOutputStream(out1File);
            inChannel = fin.getChannel();
            outChannel = fout.getChannel();
            runTime = System.currentTimeMillis();
            outChannel.transferFrom(inChannel, 0, inChannel.size());
            runTime = System.currentTimeMillis() - runTime;
            System.out.println("NO." + (i + 1) + ": "
                    + "Using transferFrom took " + runTime + "ms");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != fin) {
                try {
                    fin.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != fout) {
                try {
                    fout.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != inChannel) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != outChannel) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            out1File.deleteOnExit();
        }
        /**********************************************************/
        try {
            fin = new FileInputStream(new File(inPath));
            out2File = new File(outPath2);
            fout = new FileOutputStream(out2File);
            inChannel = fin.getChannel();
            outChannel = fout.getChannel();
            runTime = System.currentTimeMillis();
            while (true) {
                int ret = inChannel.read(bb);
                if (ret == -1) {
                    break;
                }
                bb.flip();
                outChannel.write(bb);
                bb.clear();
            }
            runTime = System.currentTimeMillis() - runTime;
            System.out.println("NO." + (i + 1) + ": " + "Using while took "
                    + runTime + "ms");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (null != fin) {
                try {
                    fin.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != fout) {
                try {
                    fout.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != inChannel) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != outChannel) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            out2File.deleteOnExit();
        }
    }
}

0 个答案:

没有答案