如何删除目录但不删除文件夹的文件

时间:2013-02-23 15:09:02

标签: java delete-file

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

@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();
    }
}

6 个答案:

答案 0 :(得分:11)

为什么不做呢

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

而不是

myFile.delete();

答案 1 :(得分:3)

只需使用File.isDirectory()

只检查它是文件还是目录。如果它是文件然后删除它,否则留下它

在你的情况下

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

答案 2 :(得分: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/"));

答案 3 :(得分:3)

使用递归非常整洁; - )

private void deleteFiles(File file) {
    if (file.isDirectory())
        for (File f : file.listFiles())
            deleteFiles(f);
    else
        file.delete();
}

答案 4 :(得分:1)

递归是过度的,因为需要后续问题这一事实证明了这一点。在那里查看我的答案更简单:

Recursive deletion causing a stack overflow error

答案 5 :(得分: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);
            }
        }
    }