Files.copy的速度变化极大

时间:2017-06-30 10:41:40

标签: java filesystems

我正在尝试在批处理期间使用Files.copy方法复制文件。每次运行都需要不同的时间来复制文件。我每次都使用完全相同的12个文件。它从30秒到30分钟不等。 怎么可能?

public void copyFile(File sourceFile, File targetFile, CopyOption... options) throws IOException {
    Files.copy(sourceFile.toPath(), targetFile.toPath(), options);
}

作为选项,我使用StandardCopyOption.COPY_ATTRIBUTES。

我过去常常使用http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java提出的代码,但是自从我升级到Java 7后就想改变它。

1 个答案:

答案 0 :(得分:0)

也许您可以尝试使用其他库?

例如Apache Commons:

Scenario: validate voucher

Given url 'http://-..uk../v3//******' 
And header Content-Type = 'application/json' 
And request {json request here}
When method post
Then status 200 



12:17:30.510 [main] DEBUG com.intuit.karate.StepDefs - response time in milliseconds: 579 12:17:30.528 [main] ERROR com.intuit.karate.StepDefs - FAILED, status code was 400, expected 200

[31mFailed scenarios:[0m [31mexamples/users/WSS.feature:3 [0m# Scenario: validate voucher

1 Scenarios ([31m1 failed[0m) 5 Steps ([31m1 failed[0m, [32m4 passed[0m) 0m3.516s

com.intuit.karate.KarateException: status code was 400, expected 200 at com.intuit.karate.StepDefs.handleFailure(StepDefs.java:516) at com.intuit.karate.StepDefs.status(StepDefs.java:447) at ✽.Then status 200(examples/users/WSS.feature:9)

或仅使用FileStream?

       /**
        * <dependency>
        * <groupId>org.apache.commons</groupId>
        * <artifactId>commons-io</artifactId>
        *  <version>1.3.2</version>
        *  </dependency>
        **/
        private void fileCopyUsingApacheCommons() throws IOException {
            File fileToCopy = new File("/tmp/test.txt");
            File newFile = new File("/tmp/testcopied.txt");

            File anotherFile = new File("/tmp/testcopied2.txt");

            FileUtils.copyFile(fileToCopy, newFile);
        }