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

∥☆過路亽.° 提交于 2020-08-25 04:24:28

问题


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.


回答1:


Your destination needs additional escape characters.

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


来源:https://stackoverflow.com/questions/29518487/fileutils-copyfile-not-creating-file-when-destination-is-a-network-path-on-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!