M.J*_*.J. 89 java directory file
在Java中,我想删除包含文件和文件夹的文件夹中存在的所有内容.
public void startDeleting(String path) {
List<String> filesList = new ArrayList<String>();
List<String> folderList = new ArrayList<String>();
fetchCompleteList(filesList, folderList, path);
for(String filePath : filesList) {
File tempFile = new File(filePath);
tempFile.delete();
}
for(String filePath : folderList) {
File tempFile = new File(filePath);
tempFile.delete();
}
}
private void fetchCompleteList(List<String> filesList,
List<String> folderList, String path) {
File file = new File(path);
File[] listOfFile = file.listFiles();
for(File tempFile : listOfFile) {
if(tempFile.isDirectory()) {
folderList.add(tempFile.getAbsolutePath());
fetchCompleteList(filesList,
folderList, tempFile.getAbsolutePath());
} else {
filesList.add(tempFile.getAbsolutePath());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用,最好的方法是什么?
Sea*_*oyd 136
如果您使用Apache Commons IO,它就是一个单行:
FileUtils.deleteDirectory(dir);
Run Code Online (Sandbox Code Playgroud)
请参见FileUtils.deleteDirectory()
Guava过去常常支持类似的功能:
Files.deleteRecursively(dir);
Run Code Online (Sandbox Code Playgroud)
几个版本之前已经从Guava中删除了它.
虽然上面的版本很简单,但它也很危险,因为它在没有告诉你的情况下做了很多假设.因此,虽然在大多数情况下它可能是安全的,但我更喜欢"官方方式"(从Java 7开始):
public static void deleteFileOrFolder(final Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return CONTINUE;
}
@Override public FileVisitResult visitFileFailed(final Path file, final IOException e) {
return handleException(e);
}
private FileVisitResult handleException(final IOException e) {
e.printStackTrace(); // replace with more robust error handling
return TERMINATE;
}
@Override public FileVisitResult postVisitDirectory(final Path dir, final IOException e)
throws IOException {
if(e!=null)return handleException(e);
Files.delete(dir);
return CONTINUE;
}
});
};
Run Code Online (Sandbox Code Playgroud)
oyo*_*oyo 84
我有这样的事情:
public static boolean deleteDirectory(File directory) {
if(directory.exists()){
File[] files = directory.listFiles();
if(null!=files){
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
}
return(directory.delete());
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
public static boolean deleteDir(File dir)
{
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i=0; i<children.length; i++)
return deleteDir(new File(dir, children[i]));
}
// The directory is now empty or this is a file so delete it
return dir.delete();
}
Run Code Online (Sandbox Code Playgroud)
它可能是嵌套文件夹的问题.您的代码按照找到的顺序删除文件夹,这是自上而下的,但不起作用.如果您先反转文件夹列表,它可能会有效.
但我建议你只使用像Commons IO这样的库.
我发现这段代码更难以理解和工作:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete(); // The directory is empty now and can be deleted.
}
Run Code Online (Sandbox Code Playgroud)