1

Using Java, I want to move a file on the file system despite if it's being used by another process? I've found this for deleting FileDeleteStrategy.FORCE.delete() is there an equivalent one for moving the file into another directory?

Kh4lid MD
  • 125
  • 1
  • 2
  • 10
JaredCS
  • 427
  • 4
  • 11
  • In what operating system? In Windows, the answer is probably no. – VGR Nov 19 '20 at 17:10
  • Yes in windows. – JaredCS Nov 19 '20 at 20:46
  • `Files.move`. I don't know what will happen on Windows if you try to move an open file. On Linux and Mac, it will work fine, at least as long as you aren't moving the file from one volume to another. – CryptoFool Nov 19 '20 at 20:49
  • Does this answer your question? [How do I move a file from one location to another in Java?](https://stackoverflow.com/questions/4645242/how-do-i-move-a-file-from-one-location-to-another-in-java) – CryptoFool Nov 19 '20 at 20:51
  • Not quite, note the "despite if its being used by another process?" part. – JaredCS Nov 19 '20 at 21:09
  • Non-programming answer that may be relevant: https://superuser.com/questions/54193/how-can-i-rename-files-and-folders-in-windows-that-are-in-use (It’s for Windows 7, but I suspect it applies to Windows 10 and Windows Server.) – VGR Nov 19 '20 at 21:13

1 Answers1

0

As others already pointed out, on Windows there is no such thing as move file while it's being used by other process. There is however standard way of handling such situation, but it will require OS restart.

From documentation of MoveFileEx:

MOVEFILE_DELAY_UNTIL_REBOOT 4 (0x4)

The system does not move the file until the operating system is restarted. The system moves the file immediately after AUTOCHK is executed, but before creating any paging files.

To use this function from java application, you could use JNA, but your application needs to be running with admin privileges with appropriate UAC.

From UX point of view, Jared Smith's answer make much more sense.

rkosegi
  • 14,165
  • 5
  • 50
  • 83