Java File Path Delete deleteFile(String p_filePath, boolean pb_recursive)

Here you can find the source of deleteFile(String p_filePath, boolean pb_recursive)

Description

delete File

License

Open Source License

Declaration

public static List<File> deleteFile(String p_filePath, boolean pb_recursive) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Lab-STICC Universite de Bretagne Sud, Lorient.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the CeCILL-B license available
 * at :/*from   w ww.j a  v a2 s . c  om*/
 * en : http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 * fr : http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
 * 
 * Contributors:
 * Dominique BLOUIN (Lab-STICC UBS), dominique.blouin@univ-ubs.fr
 ******************************************************************************/

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<File> deleteFile(String p_filePath, boolean pb_recursive) {
        List<File> deletedFiles = new ArrayList<File>();

        File file = new File(p_filePath);
        deleteFile(file, pb_recursive, deletedFiles);

        return deletedFiles;
    }

    public static void deleteFile(File p_file, boolean pb_recursive, List<File> deletedFiles) {
        if (pb_recursive && p_file.isDirectory()) {
            File[] content = p_file.listFiles();

            for (final File file : content) {
                deleteFile(file, pb_recursive, deletedFiles);
            }
        }

        deleteSingleFile(p_file, deletedFiles);
    }

    private static void deleteSingleFile(final File p_file, final List<File> deletedFiles) {
        if (p_file.isDirectory()) {
            p_file.delete();
        } else if (p_file.delete()) {
            deletedFiles.add(p_file);
        }
    }

    public static boolean deleteSingleFile(final String p_fileName) {
        return new File(p_fileName).delete();
    }
}

Related

  1. deleteFile(String filePath)
  2. deleteFile(String filePath)
  3. deleteFile(String filePath)
  4. deleteFile(String filePathAndName)
  5. deleteFile(String filePathName)
  6. deleteFile(String path)
  7. deleteFile(String path)
  8. deleteFile(String path)
  9. deleteFile(String path)