1

I am trying to copy files with the method Files.copy during a Batch. Each run it takes a different amount of time to copy the files. I use exactly the same 12 files every time. It varies from 30 seconds to 30 minutes. How is that possible?

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

As options, I use StandardCopyOption.COPY_ATTRIBUTES.

I used to use the Code proposed at http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java but wanted to change it since I upgraded to Java 7.

Clara
  • 11
  • 1
  • 2
    What environment, hardware, what other programs may be using the disk...? The possibilities are endless. – Kayaman Jun 30 '17 at 10:43
  • I am working on Windows 10 and I am running the Batch with Eclipse (Version: Mars.2 Release (4.5.2)). The files are moved from one directory on a server to another directory on the same server. I thought about the server being the Problem as well. The strange thing is that the time it takes to copy the files is consistent with the old method. – Clara Jun 30 '17 at 10:46
  • I've seen a few questions related to `Files.copy` involving odd performance behaviour, but can't remember any root reasons that were found. You should do some searching on SO for those other questions (I recommend using Google for it, as the SO search often performs poorly). – Kayaman Jun 30 '17 at 11:16
  • I've tried finding an answer to my question via Google earlier, but none of the things I found provided an answer. Most of it was "how to do it another way" rather than "why does it happen?". – Clara Jun 30 '17 at 12:53

1 Answers1

0

perhaps you can try using a different library?

for example Apache Commons:

       /**
        * <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);
        }

or just using a FileStream?

    private void fileCopyUsingFileStreams() throws IOException {
        File fileToCopy = new File("/tmp/test.txt");
        FileInputStream input = new FileInputStream(fileToCopy);

        File newFile = new File("/tmp/test_and_once_again.txt");
        FileOutputStream output = new FileOutputStream(newFile);

        byte[] buf = new byte[1024];
        int bytesRead;

        while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
        }

        input.close();
        output.close();
    }
Jose Zevallos
  • 685
  • 4
  • 3
  • I was using a FileStream in my earlier code and that worked well, so I think I'll stick to that if I can't solve the perfomance problems. I don't really want to use other librarys for such a small part of my project, but I've seen a lot of people suggesting Apache Commons. – Clara Jun 30 '17 at 12:50