Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ATOMIC_MOVE gives exceptions

Tags:

java

file

nio

I am in the middle of automating a series of actions that we do a lot in order to gain some time. This involves moving files around and starting some batches.

In this particular situation, I am trying to copy a file from one location to another location. All works fine, until I try to use the ATOMIC_MOVE copy option. This is my code:

private void copyToDropFolder(Datafile datafile, String company) throws IOException{
    Path datafilePath = datafile.getDataPath();  
    String dropFolder = locations.getLocationFor("default");
    Path dropPath = Paths.get(dropFolder, company.toUpperCase(),locations.getLocationFor("drop"), datafile.getFileName());
    Files.copy(datafilePath, dropPath, StandardCopyOption.ATOMIC_MOVE);
}

I have checked and resolved the locations of datafilePath and dropPath, they are both valid. I have tried with the other 2 standard copy options, and the program runs fine. It is only for ATOMIC_MOVE that I get an UnsupportedOperationException. It is not that I absolutely need that specific option, but I am curious why I wouldn't work. I cant really find any other reports on this issue. I am doing this on a Windows 7 machine.

Am I missing something? Or is the ATOMIC_MOVE just not supported?

like image 226
anothernoc Avatar asked Jul 03 '13 10:07

anothernoc


2 Answers

As API says, ATOMIC_MOVE is not supported for copy(), but for move() only.

like image 131
Alex Stybaev Avatar answered Oct 09 '22 11:10

Alex Stybaev


ATOMIC_MOVE is for moving operations, not copying operations.

On the other hand, you can create so-called "hard links" using Files.createLink(). There is also createSymbolicLink() but this is probably not what you want here.

And of course, .createLink() will only work if the source and destination paths are on the same filesystem.

like image 5
fge Avatar answered Oct 09 '22 12:10

fge