Force Delete all files from a folder

2020-02-27 23:35发布

I have been using a specific piece of code to delete files from a folder but it is proving very problematic because maybe I forgot to close an InputStream or two. The code I have is so big that I am not be able to see all the Inputstreams that I have not closed. Is there a way of deleting files whether there is an open InputStream or not?

This is the piece of the code that I have been using;

File fin = new File("C:/ABC Statements final/");
    File[] finlist = fin.listFiles();       
    for (int n = 0; n < finlist.length; n++) {
        if (finlist[n].isFile()) {
        System.gc();
        Thread.sleep(2000);
            finlist[n].delete();
        }
    }        

I have edited the code. This version works.

6条回答
萌系小妹纸
2楼-- · 2020-02-27 23:58
public boolean removeDir()
{
try
{
   //destFile = new File((System.getProperty("user.dir")+"/FileName"))
   // checks if the directory has any file
    File dir = destFile; 
    if (dir.isDirectory())
    {
        File[] files = dir.listFiles();
        if (files != null && files.length > 0)
        {
            for (File aFile : files) 
            {
                System.gc();
                Thread.sleep(2000);
                FileDeleteStrategy.FORCE.delete(aFile);
                System.out.println("delet file" +aFile);
            }
        }
        dir.delete();
        System.out.println("delet" +dir);
    } 
    else 
    {
        dir.delete();
    }
}
catch(Exception e)
{
    logger.log(LogStatus.FATAL, "Exception Occured While Deleting Folder : " +e);
}
return true;
}
查看更多
女痞
3楼-- · 2020-02-28 00:02
public void removeDir()
{
try
{
    File dir = new File((System.getProperty("user.dir")+"/ReportFolderToMail"+timeStamp));
    if (dir.isDirectory())
    {
        File[] files = dir.listFiles();
        if (files != null && files.length > 0)
        {
            for (File aFile : files) 
            {
                System.gc();
                Thread.sleep(2000);
                 FileDeleteStrategy.FORCE.delete(aFile);
                System.out.println("delet file" +aFile);
            }
        }
        dir.delete();
        System.out.println("delet" +dir);
    } 
    else 
    {
        dir.delete();
    }
}
catch(Exception e)
{
    e.printStackTrace();
}
查看更多
贼婆χ
4楼-- · 2020-02-28 00:09

Use:

   import org.apache.commons.io.FileUtils;
   FileUtils.cleanDirectory(fin);

Docs:

 /**
     * Clean a directory without deleting it.
     *
     * @param directory directory to clean
     * @throws IOException in case cleaning is unsuccessful
     */

In order to use it you need a dependency:

Maven:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

Gradle:

compile 'commons-io:commons-io:2.6'
查看更多
闹够了就滚
5楼-- · 2020-02-28 00:16

There is no InputStream instances in the provided chunk of code.

To not write lots of untested IO code, please take a look at the apache.commons.io project. Especially at the FileDeleteStrategy class, for file deletion operations.

Your code might look like that:

File fin = new File("C:/ABC Statements final/");

for (File file : fin.listFiles()) {
    FileDeleteStrategy.FORCE.delete(file);
}   
查看更多
闹够了就滚
6楼-- · 2020-02-28 00:16

With Apache Commons IO:

File dir = ...
FileUtils.deleteQuietly(dir);
dir.mkdirs();

No need to check for exceptions this way.

查看更多
我命由我不由天
7楼-- · 2020-02-28 00:17

You can use:

FileUtils.deleteDirectory(File directory)

from Apache Commons

查看更多
登录 后发表回答