我已经习惯了 Java 7 和新Files
类。
我正在编写一个小型应用程序,它在某些时候必须替换文件的内容。如果出现问题,我使用了一个临时文件来避免擦除目标文件。AccessDeniedException
但是,在执行实际副本时,我总是得到一个。
这是我的代码:
// Temporary file generation.
Path target = getCurrentConfigFile(); // Returns a path, works ok.
Path tempFile = Files.createTempFile("tempfile", null);
Files.write(tempFile, conf.getBytes(Charset.defaultCharset()), StandardOpenOption.WRITE);
// Actual copy.
Files.copy(tempFile, target, StandardCopyOption.REPLACE_EXISTING);
// Cleanup.
Files.delete(tempFile);
getCurrentConfigFile()
处理目标文件路径创建:
(... generates various strings from configuration parameters)
return FileSystems.getDefault().getPath(all, these, various, strings);
当我执行代码时,它是通过.bat
脚本进行的,并且我在标准命令提示符或提升中都得到了错误。目标文件位于 中C:\temp\tests
,这是我使用同一个 Windows 用户创建的目录。
似乎问题在于从临时文件中读取,因为直接写入目标是可行的。我接下来应该看哪里?