Java 7 ATOMIC_MOVE throws exceptions

I am at the center of automation of a series of actions that we do a lot to get some time. This includes moving files and running multiple batches.

In this particular situation, I am trying to copy a file from one place to another. Everything works fine until I try to use the copy parameter ATOMIC_MOVE. 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 checked and resolved the location of datafilePath and dropPath, they are both valid. I tried with two other standard copy options, and the program works fine. For ATOMIC_MOVE only, I get a UnsupportedOperationException. It is not that I absolutely need this particular option, but I am curious why I will not work. I can not find other reports on this issue. I am doing this on a windows 7 machine.

Am I missing something? Or is ATOMIC_MOVE simply not supported?

+6
source share
2 answers

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

+14
source

ATOMIC_MOVE is for move operations, not copy operations.

Alternatively, you can create so-called "hard links" using Files.createLink() . There is also createSymbolicLink() , but that is probably not what you want here.

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

+2
source

Source: https://habr.com/ru/post/948619/


All Articles