简体   繁体   中英

How to delete files of a directory but not the folders

I have create some code that deletes all the files in a folder, the issue is while this is great, I want to be able to delete all the files in a directory but leave the folders intact, so I do not have to go into each folder and delete the files in it, this is my current code :

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

    }
}

How could I modify my code to achieve this aim?

EDIT :

I have changed my code to :

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

    }

And while this does call all the folders, this is my console:

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 does not delete the files inside these folders, how can I do this?

EDIT 3: Using recursion I am now getting a stack overflow error.

Here's my code :

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

Why not just do

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

instead of

myFile.delete();

?

simply just use File.isDirectory() .

just check whether it is file or directory. if it is file then delete it otherwise leave it

in your case

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

I think this will do

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)
        }
    }
}

Then you need to call deleteFiles(new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/")) ;

Using recursion it's very neat ;-)

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

Recursion is overkill, as evidenced by the fact that a follow-up question was needed. See my answer there for a much simpler way:

Recursive deletion causing a stack overflow error

you can use the below method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM