2

我创建了一些代码来删除文件夹中的所有文件,问题是虽然这很好,但我希望能够删除目录中的所有文件但保持文件夹完整,所以我不必进入每个文件夹并删除其中的文件,这是我当前的代码:

@ViewScoped
@ManagedBean
public class Delete {

    public void DeleteFiles() throws IOException {
        System.out.println("Called deleteFiles");
        File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/up617648/");

        String[] myFiles;
        if (file.isDirectory()) {
            myFiles = file.list();
            for (int i = 0; i < myFiles.length; i++) {
                File myFile = new File(file, myFiles[i]);
                System.out.println(myFile);
                myFile.delete();
            }
            DeleteFiles2();
        }

    }
    public void DeleteFiles2() throws IOException {
        System.out.println("Called deleteFiles2");
        File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/up617648/");

        String[] myFiles;
        if (file.isDirectory()) {
            myFiles = file.list();
            for (int i = 0; i < myFiles.length; i++) {
                File myFile = new File(file, myFiles[i]);
                System.out.println(myFile);
                myFile.delete();
            }
        }

    }
}

我怎样才能修改我的代码来实现这个目标?

编辑 :

我已将代码更改为:

@ViewScoped
@ManagedBean
public class Delete {

    public void DeleteFiles() throws IOException {
        System.out.println("Called deleteFiles");
        File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");

        String[] myFiles;
        if (file.isDirectory()) {
            myFiles = file.list();
            for (int i = 0; i < myFiles.length; i++) {
                File myFile = new File(file, myFiles[i]);
                System.out.println(myFile);
               if (!myFile.isDirectory()) {
                    myFile.delete();
                }
            }
            DeleteFiles2();
        }

    }

虽然这确实调用了所有文件夹,但这是我的控制台:

INFO: Called deleteFiles
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\cam01342
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\geg00061
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\null
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\up617648
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\web\resources\pdf\up626088
INFO: Called deleteFiles2

Tt 不会删除这些文件夹中的文件,我该怎么做?

编辑 3:使用递归我现在收到堆栈溢出错误。

这是我的代码:

public void DeleteFiles() {
    File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
    System.out.println("Called deleteFiles");
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            DeleteFiles();
        }
    } else {
        file.delete();
    }
}
4

6 回答 6

11

为什么不只是做

if (!myFile.isDirectory()) myFile.delete();

代替

myFile.delete();

?

于 2013-02-23T15:11:36.253 回答
3

使用递归非常简洁;-)

private void deleteFiles(File file) {
    if (file.isDirectory())
        for (File f : file.listFiles())
            deleteFiles(f);
    else
        file.delete();
}
于 2013-02-23T15:22:46.070 回答
3

只需使用 File.isDirectory().

只需检查它是文件还是目录。如果是文件,则删除它,否则保留它

在你的情况下

if (!(myFile.isDirectory()))
{
 myFile.delete();
}
于 2013-02-23T15:12:04.143 回答
3

我认为这会做

public void deleteFiles(File folder) throws IOException {
    List<File> files = folder.listFiles()
    foreach(File file: files){
        if(file.isFile()){
            file.delete();
        }else if(file.isDirecotry()) {
            deleteFiles(file)
        }
    }
}

然后你需要打电话deleteFiles(new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/"))

于 2013-02-23T15:13:49.507 回答
1

递归是矫枉过正的,需要一个后续问题就证明了这一点。请参阅我的答案以获得更简单的方法:

递归删除导致堆栈溢出错误

于 2013-02-23T16:30:06.960 回答
0

您可以使用以下方法。

public void deleteFiles(File folder) throws IOException {
    File[] files = folder.listFiles();
     for(File file: files){
            if(file.isFile()){
                String fileName = file.getName();
                boolean del= file.delete();
                System.out.println(fileName + " : got deleted ? " + del);
            }else if(file.isDirectory()) {
                deleteFiles(file);
            }
        }
    }
于 2016-02-03T09:45:28.423 回答