Java Delete Directory deleteFiles(String[] filenames)

Here you can find the source of deleteFiles(String[] filenames)

Description

Clean up - delete unzipped files that have already been included into ZIP

License

Apache License

Declaration

public static void deleteFiles(String[] filenames) 

Method Source Code

//package com.java2s;
/**************************************************************************************
Copyright 2015 Applied Research Associates, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the License
at://from w  ww.j  a  va 2  s  .  co m
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
**************************************************************************************/

import java.io.File;

public class Main {
    /**
     * Clean up - delete unzipped files that have already been included into ZIP
     */
    public static void deleteFiles(String[] filenames) {
        File file;
        if (filenames == null)
            return;
        for (int i = 0; i < filenames.length; i++) {
            file = new File(filenames[i]);
            if (file.exists())
                file.delete();
        }
    }

    /**
     * Delete files in a specified directory
     * @param fileNames
     */
    public static void deleteFiles(String[] fileNames, String directory) {
        File dir = new File(directory);

        //delete files
        for (int i = 0; i < fileNames.length; i++) {
            dir = new File(directory + "/" + fileNames[i]);
            if (dir.exists())
                dir.delete();
        }
    }

    public static boolean delete(String fileName) {
        return delete(new File(fileName));
    }

    public static boolean delete(File f) {
        if (f.isDirectory()) {
            for (File c : f.listFiles()) {
                delete(c);
            }
        }
        return f.delete();
    }
}

Related

  1. deleteFiles(String directory, FileFilter filter, boolean recursive)
  2. deleteFiles(String directoryName)
  3. deleteFiles(String fileName)
  4. deleteFiles(String root, String mask)
  5. deleteFiles(String sDir, String sSearch)
  6. deleteFilesAndDir(List files)
  7. deleteFilesAndSubDirs(File dir)
  8. deleteFilesFromDirectory(final String dirNameIn, final String filePrefixIn)
  9. deleteFilesInDir(File dir)