javawindowsapache-commons-io

FileUtils.copyFile() not creating file when destination is a network path (on windows)


I'm using apache common's FileUtils.copyFile() to copy a file on a local disk to a network share location. The shared folder already exists, and the user running the app has permission to it. FileUtils.copyFile() executes with no exceptions. However, the file doesn't actually get created.

File sourceFile = new File ("C:\\sourcefile.txt");
File destinationFile = new File("\\data-server\\my_share\\dest.txt");
// false
System.out.println("Before copy, file exists? " + destinationFile.exists());
FileUtils.copyFile(sourceFile, destinationFile);
// true
System.out.println("After copy, file exists? " + destinationFile.exists());

With the network share path specified as the destination, it doesn't work. But, if I map the network drive in windows and write to it via the network map, it works. What's very odd is that I call file.exists() after the copy operation, and java reports that the file exists, but it doesn't show up.

I also tried using FIleUtils.copyFileToDirectory(), just specifying the destination directory and not the file name. I'm getting the same issue when the destination is the network path.


Solution

  • Your destination needs additional escape characters.

    "\\\\data-server\\my_share\\dest.txt"