Description
Deletes a given File , if it exists.
License
Apache License
Parameter
Parameter | Description |
---|
path | Reference to the File to be deleted |
Exception
Parameter | Description |
---|
FileNotFoundException | When the given File does not exist |
NullPointerException | When the given File reference is null |
Return
True, if it successfully deleted the given . False, otherwise.
Declaration
static boolean deleteDir(File path) throws FileNotFoundException, NullPointerException
Method Source Code
//package com.java2s;
/*/*w w w . jav a2 s .co m*/
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* 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;
import java.io.FileNotFoundException;
public class Main {
/**
* Deletes a given {@link File}, if it exists. If it doesn't exist, it throws a {@link FileNotFoundException}
* If the given {@link File} is a directory, it recursively deletes the files in the directory, before deleting the
* directory itself.
*
* @param path Reference to the {@link File} to be deleted
* @return True, if it successfully deleted the given {@link File}. False, otherwise.
* @throws FileNotFoundException When the given {@link File} does not exist
* @throws NullPointerException When the given {@link File} reference is null
*/
static boolean deleteDir(File path) throws FileNotFoundException, NullPointerException {
if (path == null) {
throw new NullPointerException("Path cannot be null!");
}
if (!path.exists()) {
throw new FileNotFoundException("File not found: " + path);
}
boolean result = true;
if (path.isDirectory()) {
for (File f : path.listFiles()) {
result = result & deleteDir(f);
}
}
return result && path.delete();
}
}
Related
- deleteDir(File f)
- deleteDir(File fDir)
- deleteDir(File file)
- deleteDir(File path)
- deleteDir(File path)
- deleteDir(File path)
- deleteDir(final String path)
- deleteDir(String path)
- deleteDir(String path)