Java Delete File or Directory deleteFileOrDir(File instanceFileOrDir)

Here you can find the source of deleteFileOrDir(File instanceFileOrDir)

Description

delete File Or Dir

License

Open Source License

Declaration

public static void deleteFileOrDir(File instanceFileOrDir) 

Method Source Code

//package com.java2s;
/* MOD_V2.0//from  w  w  w  .  j  a  v a2  s .c o m
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 * 
 * This file is part of OpenDA. 
 * 
 * OpenDA is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * OpenDA is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    public static void deleteFileOrDir(File instanceFileOrDir) {
        if (instanceFileOrDir.isDirectory()) {
            deleteDirectory(instanceFileOrDir);
        } else {
            if (!instanceFileOrDir.delete()) {
                throw new RuntimeException(
                        "BB Stoch model factory: Could not delete instance file "
                                + instanceFileOrDir.getAbsolutePath());
            }
        }
    }

    public static void deleteDirectory(File directory) {

        if (directory.getPath().length() <= 4) {
            throw new IllegalArgumentException("Path length is too short");
        }

        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                deleteDirectory(file);
            } else {
                boolean success = file.delete();
                //                if (!success) {
                //                    throw new RuntimeException("Could not delete file " + file.getAbsolutePath());
                //                }
            }
        }
        boolean success = directory.delete();
        //        if (!success) {
        //            throw new RuntimeException("Could not delete directory " + directory.getAbsolutePath());
        //        }
    }
}

Related

  1. deleteFileOrDir(File dir, int max)
  2. deleteFileOrDir(File fd)
  3. deleteFileOrDir(File file)
  4. deleteFileOrDir(File file)
  5. deleteFileOrDir(File filehandle)
  6. deleteFileOrDirectory(File dir)
  7. deleteFileOrDirectory(File file)
  8. deleteFileOrDirectory(File fileOrDirectory)
  9. deleteFileOrDirectory(String fileName)