Java File Path Delete delAllFiles(File dir, String prefix)

Here you can find the source of delAllFiles(File dir, String prefix)

Description

del All Files

License

Open Source License

Parameter

Parameter Description
dir a parameter
prefix a parameter

Declaration

public static boolean delAllFiles(File dir, String prefix) 

Method Source Code

//package com.java2s;
/*//from   w w w.  jav a 2s. c  o m
 * Copyright (c) Fiorano Software Pte. Ltd. and affiliates. All rights reserved. http://www.fiorano.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.*;

public class Main {
    /**
     * @param dir
     * @param prefix
     * @return
     */
    public static boolean delAllFiles(File dir, String prefix) {
        boolean bUnableToDeleteSomeFiles = false;

        String[] fileList = dir.list();
        String path = dir.getAbsolutePath() + File.separator;
        File file;

        for (int i = 0; i < fileList.length; ++i) {
            if (fileList[i].startsWith(prefix)) {
                try {
                    file = new File(path + fileList[i]);
                    file.delete();
                } catch (Exception e) {
                    //  No problem continue with the cleanup of
                    //  rest of the files.
                    //
                    bUnableToDeleteSomeFiles = true;
                }
            }
        }

        return bUnableToDeleteSomeFiles;
    }
}

Related

  1. delAll(String path)
  2. delAllFile(String path)
  3. delAllFiles(File dir, String suffix)
  4. delDir(String path)
  5. delDirAndFile(String tempath)
  6. deleleFiles(String regex, String path)