Java删除文件的更好方法(如果存在)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27599965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java better way to delete file if exists
提问by Michal Chmi
We need to call file.exists()
before file.delete()
before we can delete a file E.g.
我们需要调用file.exists()
beforefile.delete()
才能删除文件 例如
File file = ...;
if (file.exists()){
file.delete();
}
Currently in all our project we create a static method in some util class to wrap this code. Is there some other way to achieve the same , so that we not need to copy our utils file in every other project.
目前,在我们所有的项目中,我们在一些 util 类中创建了一个静态方法来包装此代码。有没有其他方法可以实现相同的目的,这样我们就不需要在每个其他项目中复制我们的 utils 文件。
采纳答案by sol4me
Starting from Java 7 you can use deleteIfExiststhat returns a boolean (or throw an Exception) depending on whether a file was deleted or not. This method may not be atomic with respect to other file system operations. Moreover if a file is in use by JVM/other program then on some operating system it will not be able to remove it. Every file can be converted to path via toPath
method . E.g.
从 Java 7 开始,您可以使用deleteIfExists返回布尔值(或抛出异常),具体取决于文件是否被删除。对于其他文件系统操作,此方法可能不是原子的。此外,如果 JVM/其他程序正在使用文件,那么在某些操作系统上它将无法删除它。每个文件都可以通过toPath
方法转换为路径。例如
File file = ...;
boolean result = Files.deleteIfExists(file.toPath()); //surround it in try catch block
回答by Eugen
There's also the Java 7 solution, using the new(ish) Path abstraction:
还有 Java 7 解决方案,使用 new(ish) Path 抽象:
Path fileToDeletePath = Paths.get("fileToDelete_jdk7.txt");
Files.delete(fileToDeletePath);
Hope this helps.
希望这可以帮助。
回答by Martijn Courteaux
At my machine, I can just do:
在我的机器上,我可以这样做:
file.delete()
if the file doesn't exist, it will return false.
如果文件不存在,它将返回false。
回答by java_joe
This is my solution:
这是我的解决方案:
File f = new File("file.txt");
if(f.exists() && !f.isDirectory()) {
f.delete();
}
回答by everblack
I was working on this type of function, maybe this will interests some of you ...
我正在研究这种类型的功能,也许这会让你们中的一些人感兴趣......
public boolean deleteFile(File file) throws IOException {
if (file != null) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f: files) {
deleteFile(f);
}
}
return Files.deleteIfExists(file.toPath());
}
return false;
}
回答by yogesh rajguru
File xx = new File("filename.txt");
if (xx.exists()) {
System.gc();//Added this part
Thread.sleep(2000);////This part gives the Bufferedreaders and the InputStreams time to close Completely
xx.delete();
}
回答by RANA DINESH
Use the below statement to delete any files:
使用以下语句删除任何文件:
FileUtils.forceDelete(FilePath);
Note:Use exception handling
codes if you want to use.
注意:exception handling
如果要使用,请使用代码。
回答by S Williams
Apache Commons IO's FileUtilsoffers FileUtils.deleteQuietly
:
Apache Commons IO 的FileUtils提供FileUtils.deleteQuietly
:
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- No exceptions are thrown when a file or directory cannot be deleted.
删除文件,从不抛出异常。如果文件是一个目录,删除它和所有子目录。File.delete() 和这个方法的区别是:
- 要删除的目录不必为空。
- 无法删除文件或目录时不会引发异常。
This offers a one-liner delete call that won't complain if the file fails to be deleted:
这提供了一个单行删除调用,如果文件无法删除,它不会抱怨:
FileUtils.deleteQuietly(new File("test.txt"));
回答by Sourabh Bhavsar
Use Apache Commons FileUtils.deleteDirectory()or FileUtils.forceDelete()to log exceptions in case of any failures,
使用 Apache Commons FileUtils.deleteDirectory()或FileUtils.forceDelete()在出现任何故障时记录异常,
or FileUtils.deleteQuietly()if you're not concerned about exceptions thrown.
或者FileUtils.deleteQuietly()如果你不关心抛出的异常。