Java 是否有移动和覆盖文件的操作?
我正在寻找移动和覆盖文件的操作。我知道在中有一种新方法,但我希望了解Java7。我也知道和中的方法,但是FileUtils不会覆盖,而Guava one不会记录它 我也知道,我可以写我自己的方法,好吧,我开始,但看到一些问题在这里和那里,所以我希望已经做了一些事情Java 是否有移动和覆盖文件的操作?,java,io,Java,Io,我正在寻找移动和覆盖文件的操作。我知道在中有一种新方法,但我希望了解Java7。我也知道和中的方法,但是FileUtils不会覆盖,而Guava one不会记录它 我也知道,我可以写我自己的方法,好吧,我开始,但看到一些问题在这里和那里,所以我希望已经做了一些事情 你有什么建议吗?我使用以下方法: public static void rename(String oldFileName, String newFileName) { new File(newFileName).delete
你有什么建议吗?我使用以下方法:
public static void rename(String oldFileName, String newFileName) {
new File(newFileName).delete();
File oldFile = new File(oldFileName);
oldFile.renameTo(new File(newFileName));
}
如果您要继续编写自己的实用程序,您可能需要查看Ant中的
复制任务,因为它支持覆盖。我已经完成了编写自己的方法,对于所有对可能的解决方案感兴趣的人,我使用了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;
}
}
使用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);
}
Apache FileUtils JavaDoc for FileUtils.copyFileToDirectory说,“如果目标文件存在,则此方法将覆盖它。”复制后,您可以在删除之前进行验证
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;
}
您还可以使用诸如启用对现有文件系统的事务性访问之类的工具
apache还有一个替代方案:
对我有效的最短解决方案:
File destFile = new File(destDir, file.getName());
if(destFile.exists()) {
destFile.delete();
}
FileUtils.moveFileToDirectory(file, destDir, true);
:
用字节数组的内容覆盖文件
:
警告:如果to表示现有文件,则该文件将被删除
被来自的内容覆盖。如果是往返,请参阅
同一个文件,该文件的内容将被删除
使用复制
。所以可以放心地假设它也会覆盖。你真的试过番石榴吗?@nhahdh不,我没有试过番石榴。但是我声明要实现unixmv,如果没有提示,mv将不会覆盖。谢谢,这是我的第一个实现,但是我的文件相当大。因此,如果在删除第一个文件后程序将中断,则所有数据都将丢失或散布到某个地方。编辑:这个问题出现了,这就是我问的原因。所以我需要修复这个问题。我知道这是一篇旧文章,但我目前正在使用此方法,它不会按照作者的要求覆盖,这也是为什么我要替换它谢谢你的链接,但我希望有一个现有的解决方案。什么是CSVUtils
?该类的名称。这些都是静态方法,和作者上面说的一样。在移动文件之前,他无法删除:(
Files.write(bytes, new File(path));