4

I'm trying to copy a file from one directory to another, but I want the call to be blocking so that the program only proceeds execution once the whole file has been transferred.

Currently I'm using Files.copy(), which uses non-blocking IO and returns instantly. What other options (preferably built into the java standard library) do I have to solve this problem?

Many thanks, Gareth

Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35
  • 1
    I see nothing in the [`Files.copy` javadoc](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.nio.file.Path-java.nio.file.Path-java.nio.file.CopyOption...-) that says the operation is asynchronous. Normally an asynchronous operation accepts some form of callback (a lambda, or an object implementing an interface that it will call), or returns something that can be used to check for completion. `Files.copy` just returns a `Path`. Due respect, I think you're already using what you want, and just misunderstanding something. – T.J. Crowder Apr 11 '14 at 10:38
  • I'm new to java, so I may well be misunderstanding something. [This](http://stackoverflow.com/a/106807/1199721) prevoius answer says it defers the request to the OS, and the NIO package that it's in also leads me to believe that it would be non-blocking – Pep_8_Guardiola Apr 11 '14 at 10:44
  • 1
    @ Gareth: I just checked -- `Files.copy` doesn't return until the copy is complete. – T.J. Crowder Apr 11 '14 at 10:47

3 Answers3

13

Files.copy isn't asynchronous, it doesn't return until the copy is complete.

Normally if a method is asynchronous, the JavaDoc will be very clear about that, and the method will accept some form of callback (a lambda, or an object implementing an interface with a method it'll call, etc.), or return something that can be used to check for completion (or error). Files.copy doesn't do that, and its Javadoc doesn't say it's asynchronous, so there's no reason to suspect it is. (I just did a quick test to be sure, and it didn't return until the copy was complete.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the info - my problem must be elsewhere! – Pep_8_Guardiola Apr 11 '14 at 10:50
  • @GarethWebber even I am facing a similar problem but haven't been able to identify the Cause. Do you remember what was the issue that you were facing? – Limit Apr 19 '16 at 09:27
  • @Limit Same boat as you were in. Did you find the culprit? I'm using a the java API file WatchService. After I call `Files.copy` the watchservice fires a new file event, but the file is empty at this point. A few seconds later the file is completed. – flakes May 26 '17 at 22:38
  • @Limit nevermind, I figured it out. This answer helped a lot [answer](https://stackoverflow.com/a/22956548/3280538). I was creating the file by reading from an tcp inputstream which made it slow to finish copying. To avoid the bug I load the stream into a tmp file and then copy the tmp file to the destination path. No more bugs. :) – flakes May 26 '17 at 22:56
  • It is correct for the JVM, but once the data "move" to the OS itself, it is not guarantee that the data was stored on the underlying storage device. For example, in Linux OS you will need to call sync to guarantee the data stored on the storage device itself and not on the cache. Alternative to sync call (for cross platform support) you can use FileDescriptor.sync(), but then you will need to copy file by file and retrieve its FileDescriptor each. – Tamir Adler Jan 19 '21 at 16:16
  • I have had the same issue, Java 11 and CentOS-7. Occasionaly, after Files.copy, Files.exists can return true, but trying to read the file results in FileNotFoundException. As others have found, copying to a tmp filename and then doing a move seems to have resolved the problem. – cmgharris Aug 25 '22 at 12:40
1

For synchronous copying, you can use streams to copy from one file descriptor to another. For more info check out the following link: http://www.mkyong.com/java/how-to-copy-file-in-java/

Akhil Raina
  • 379
  • 2
  • 9
0

Look at java.io package for your answer. The File class operations there are blocking, as far as I remember.

An SO User
  • 24,612
  • 35
  • 133
  • 221