是否有移动和覆盖文件的操作?

我正在寻找一个移动和覆盖文件的操作。 我知道Java7中有一个新方法,但我希望能够绕过Java7。 我也知道FileUtils和Guava中的方法,但FileUtils不会覆盖,而Guava也不会记录它。

我也知道,我可以编写自己的方法,我开始了,但是在这里和那里看到了一些问题,所以我希望已经完成了一些事情。

你有什么建议吗?

我使用以下方法:

public static void rename(String oldFileName, String newFileName) { new File(newFileName).delete(); File oldFile = new File(oldFileName); oldFile.renameTo(new File(newFileName)); } 

我已经完成了编写自己的方法,对于每个对可能的解决方案感兴趣的人,我使用了ApacheCommons FileUtils,这也许并不完美,但对我来说效果还不错:

 /** * Will move the source File to the destination File. * The Method will backup the dest File, copy source to * dest, and then will delete the source and the backup. * * @param source * File to be moved * @param dest * File to be overwritten (does not matter if * non existent) * @throws IOException */ public static void moveAndOverwrite(File source, File dest) throws IOException { // Backup the src File backup = CSVUtils.getNonExistingTempFile(dest); FileUtils.copyFile(dest, backup); FileUtils.copyFile(source, dest); if (!source.delete()) { throw new IOException("Failed to delete " + source.getName()); } if (!backup.delete()) { throw new IOException("Failed to delete " + backup.getName()); } } /** * Recursive Method to generate a FileName in the same * Folder as the {@code inputFile}, that is not existing * and ends with {@code _temp}. * * @param inputFile * The FileBase to generate a Tempfile * @return A non existing File */ public static File getNonExistingTempFile(File inputFile) { File tempFile = new File(inputFile.getParentFile(), inputFile.getName() + "_temp"); if (tempFile.exists()) { return CSVUtils.getNonExistingTempFile(tempFile); } else { return tempFile; } } 

用于FileUtils.copyFileToDirectory的Apache FileUtils JavaDoc说:“如果目标文件存在,那么此方法将覆盖它。” 复制后,您可以在删除之前进行validation。

 public boolean moveFile(File origfile, File destfile) { boolean fileMoved = false; try{ FileUtils.copyFileToDirectory(origfile,new File(destfile.getParent()),true); File newfile = new File(destfile.getParent() + File.separator + origfile.getName()); if(newfile.exists() && FileUtils.contentEqualsIgnoreCaseEOL(origfile,newfile,"UTF-8")) { origfile.delete(); fileMoved = true; } else { System.out.println("File fail to move successfully!"); } }catch(Exception e){System.out.println(e);} return fileMoved; } 

最短的解决方案,对我有用:

 File destFile = new File(destDir, file.getName()); if(destFile.exists()) { destFile.delete(); } FileUtils.moveFileToDirectory(file, destDir, true); 

如果您将继续编写自己的实用程序,您可能需要查看Ant中copy任务的实现 ,因为它支持覆盖。

使用Apache Commons FileUtils:

  try { FileUtils.moveFile(source, dest); print("------------------------------"); print(name + " moved to " + PropertiesUtil .getProperty(PropertiesUtil.COMPLETED_PATH)); } catch (FileExistsException fe){ if(dest.delete()){ try { FileUtils.moveFile(source, dest); } catch (IOException e) { logger.error(e); } print("------------------------------"); print(name + " moved to " + PropertiesUtil .getProperty(PropertiesUtil.COMPLETED_PATH)); } } catch (Exception e) { logger.error(e); } 

如图所示,可以使用预删除目标实现具有重写方法的纯Java nio解决方案移动

 public void move(File sourceFile, String targetFileName) { Path sourcePath = sourceFile.toPath(); Path targetPath = Paths.get(targetFileName); File file = targetFile.toFile(); if(file.isFile()){ Files.delete(destino); } Files.move(origen, destino); } 

您还可以使用像https://xadisk.java.net/这样的工具来启用对现有文件系统的事务访问。

还有一个来自apache的替代方案:

https://commons.apache.org/proper/commons-transaction/apidocs/org/apache/commons/transaction/file/FileResourceManager.html

Guava Files.write :

使用字节数组的内容覆盖文件

 Files.write(bytes, new File(path)); 

Files.copy :

警告:如果要表示现有文件,则该文件将被from的内容覆盖。 如果to和from引用同一文件,则将删除该文件的内容。

Files.move使用copy 。 因此可以安全地假设它也会被覆盖。