首页 > 解决方案 > File.delete() 不是实际的

问题描述

我想删除一个文件夹及其内容。如果文件结构如下:

 TestFolder
    -Folder1
        --File1
    -Folder2
        --File2
        --File3

数组应按以下顺序包含文件:{Folder1,Folder2,File1,Folder3,File2,File3},使用以下代码:

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;

class Folder{
    private int filesCount = 0;
    private File[] files;
    private int counter = 0;

    private void getFilesArray() throws IOException{
        paths = new String[countFiles(path)];
        thisFolder(path);
    }

    private void countFiles(File folder) throws IOException{
        File[] content = (new File(folder)).listFiles();
        for (File file:content){    
            if (file.isDirectory()){
                countFiles(file);
            }
            filesCount++;
        }
    }

    private void thisFolder(File folder) throws IOException{
        if (folder.exists()){
            File[] content = file.listFiles();
            for (File file:content){
                files[counter] = file;
                counter++;
                if (file.isDirectory()){
                    thisFolder(file);
                }
            }
        }else{
            throw new IOException("Folder was deleted while it was still opened:    "+file.getAbsolutePath());
        }
    }
    //...
}

我想使用 File[]-Array 删除文件夹的内容:

public boolean delete(){
    for (int count = filesCount-1; count >= 0;count--){
        if(files[count].exists()){
            if(files[count].delete()==false){
                System.out.println("Failed to delete    "+files[count]);
                for (String temp:files[count].list())
                    System.out.println("    remaining content   "+temp);
                return false;
            }else{
                System.out.println("Properly deleted    "+files[count].getAbsolutePathPath());
            }
        }
    }
    if (folder.delete()){
        return true;
    }else{
        System.out.println(2);
        return false;
    }
}

我需要多个其他方法中的 File[]-Array 并且实际上希望我正在创建的文件夹对象包含我正在创建它的那一刻的-也许是旧的-内容。如果不工作,删除方法将返回 false。错误处理属于不同的类别。现在的问题:

它会在第一次运行时删除所有文件属性以及在我运行程序之前为空的每个文件夹,但它无法删除在我运行程序之前包含内容的文件夹,即使它们现在是空的。

这里是调试输出(第一次运行):

//Properly deleted        Folder2\File3.txt
//Properly deleted        Folder2\File2.txt
//Failed to delete        Folder2
//  remaining content       File2.txt
//  remaining content       File3.txt
//program aborted

这里是调试输出(第二次运行):

//Properly deleted        Folder2
//Properly deleted        Folder1\File1.txt
//Failed to delete        Folder1
//  remaining content       File1.txt
//program aborted

这里是调试输出(第三次运行):

//Properly deleted        Folder1
//Properly deleted        TestFolder
//program completed

该目录 - 当然 - 仅供程序测试,所以我确信其中没有其他(隐藏)文件。有 - 对于测试 - 也没有不同的程序或方法使用文件夹内容,所以即使当我将代码用于我的程序时它可能会有所不同,对于这个测试,我绝对确定文件 [ ]-数组是真实的!我什至从下面的评论中测试了递归方法,但仍然遇到同样的问题,即使调试输出有点不同,因为我访问文件和文件夹的顺序不同。有什么技巧可以使文件再次变为现实吗?也许像 File.update(); 这样的命令

谢谢你的帮助 :)

标签: javafile

解决方案


你跑偏了。首先,放下睡眠。这不是必需的。

接下来,您有多确定以正确的顺序删除?我个人不会像您那样删除它。我会创建一个删除文件及其内容的递归方法。

void deleteMe(File file) {
    if (file.isDirectory()) {
         for (File subfile: file.listFiles) {
             deleteMe(subfile);
         }
    }
    file.delete();
}

为了调试上面的代码,在调用 delete 之前,我可能会添加如下调试:

System.out.printf("File: %s. Number of subfiles: %d\n", file.getName(), file.listFiles().length).

(类似的东西。)

只是为了确保它正在做你认为它正在做的事情。


推荐阅读