Java move(路径源、路径目标、CopyOption…选项)将从源中消失文件
在Windows和Linux上,我使用java.nio.file.Files.move(路径源、路径目标、CopyOption…选项)方法将文件从源移动到目标 代码:Java move(路径源、路径目标、CopyOption…选项)将从源中消失文件,java,nio,Java,Nio,在Windows和Linux上,我使用java.nio.file.Files.move(路径源、路径目标、CopyOption…选项)方法将文件从源移动到目标 代码: public void purgeProcessedFile(File xmlFile, String fileDestination) { logger.info("Started purging."); File directory = new File(fileDestination);
public void purgeProcessedFile(File xmlFile, String fileDestination) {
logger.info("Started purging.");
File directory = new File(fileDestination);
if (!directory.exists()) {
directory.mkdirs();
}
File destFile = new File(fileDestination + File.separator + xmlFile.getName());
logger.info("XML file path is : " + xmlFile.getPath());
logger.info("dest File path is : " + destFile.getPath());
if (!destFile.exists()) {
//if (xmlFile.renameTo(destFile)) {
try {
Files.move(FileSystems.getDefault().getPath(xmlFile.getPath()),
FileSystems.getDefault().getPath(destFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
//throw LoggedException.wrap(logger, "Unexpected IOException", e);
logger.info("File purging failed.");
logger.error(e);
}
logger.info("File purged successfully.");
/* } else {
logger.info("File purging failed.");
}*/
} else {
logger.info("File with same name already exists at destination folder: " + fileDestination);
}
logger.info("Ended purging.");
}
预期结果:
public void purgeProcessedFile(File xmlFile, String fileDestination) {
logger.info("Started purging.");
File directory = new File(fileDestination);
if (!directory.exists()) {
directory.mkdirs();
}
File destFile = new File(fileDestination + File.separator + xmlFile.getName());
logger.info("XML file path is : " + xmlFile.getPath());
logger.info("dest File path is : " + destFile.getPath());
if (!destFile.exists()) {
//if (xmlFile.renameTo(destFile)) {
try {
Files.move(FileSystems.getDefault().getPath(xmlFile.getPath()),
FileSystems.getDefault().getPath(destFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
//throw LoggedException.wrap(logger, "Unexpected IOException", e);
logger.info("File purging failed.");
logger.error(e);
}
logger.info("File purged successfully.");
/* } else {
logger.info("File purging failed.");
}*/
} else {
logger.info("File with same name already exists at destination folder: " + fileDestination);
}
logger.info("Ended purging.");
}
要移动到目标目录的文件
实际结果:
public void purgeProcessedFile(File xmlFile, String fileDestination) {
logger.info("Started purging.");
File directory = new File(fileDestination);
if (!directory.exists()) {
directory.mkdirs();
}
File destFile = new File(fileDestination + File.separator + xmlFile.getName());
logger.info("XML file path is : " + xmlFile.getPath());
logger.info("dest File path is : " + destFile.getPath());
if (!destFile.exists()) {
//if (xmlFile.renameTo(destFile)) {
try {
Files.move(FileSystems.getDefault().getPath(xmlFile.getPath()),
FileSystems.getDefault().getPath(destFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
//throw LoggedException.wrap(logger, "Unexpected IOException", e);
logger.info("File purging failed.");
logger.error(e);
}
logger.info("File purged successfully.");
/* } else {
logger.info("File purging failed.");
}*/
} else {
logger.info("File with same name already exists at destination folder: " + fileDestination);
}
logger.info("Ended purging.");
}
文件正在从源目录删除,而不是移动到目标目录
在日志中
java.nio.file.NoSuchFileException: source location.
他被扔了
由于这是独立于平台的,因此期望能够在windows和Linux上工作。可能是
FileSystems.getDefault()
对网络驱动器无效。
在新的Path/Path/Files
样式中:
public void purgeProcessedFile(File xmlFile, String fileDestination) {
Path destFile = Paths.get(fileDestination, xmlFile.getName());
Path directory = destFile.getParent();
if (!Files.exist(directory)) {
Files.createDirectories(directory);
}
if (!Files.exists(destFile)) {
try {
Files.move(xmlFile.toPath(), destFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
logger.error(e);
}
} else {
logger.info("File with same name already exists at destination folder: "
+ fileDestination);
}
}
也就是说,我希望消失的文件驻留在本地计算机上创建的目录中,可能类似于C:/F/My/XML/Data。您确定源文件的存在吗?错误为“源”显示“没有这样的文件”。@Santosh是的,我已将文件放在源位置。我敢肯定。你应该能够把代码转换成MCVE。。。所以其他人可以试着运行它。@Stephen C这个方法就是我遇到的问题,我不确定我还能在这里添加什么?如果你把它变成MVCE,其他人就可以运行它了。明白了吗?destFile.getPath()和xmLFile.getPath()返回字符串,但是move方法需要路径类型参数。很抱歉,复制错误,已更正。xml文件。toPath:将文件更改为路径。现在应该可以了。在fileDestination上有点混淆。对于正在发生此编译器错误的类型文件,方法exist(Path)未定义。抱歉,已将其更改为Files。exists位于on位置,而不是其他位置。