How to delete files of a directory but not the folders

后端 未结 6 2122
死守一世寂寞
死守一世寂寞 2021-01-15 12:48

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 fold

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-15 13:25

    I think this will do

    public void deleteFiles(File folder) throws IOException {
        List 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/"));

提交回复
热议问题