Here you can find the source of deleteIfExists(File file)
#delete delete (file)
if file != null and file exists.
Parameter | Description |
---|---|
file | a normal file that is to be deleted; may be null or non-existent |
Parameter | Description |
---|---|
IllegalArgumentException | if file is not a normal file |
SecurityException | if a security manager exists and denies read access to the file |
IOException | if an I/O problem occurs; this includes failure of the file to be deleted |
public static void deleteIfExists(File file) throws IllegalArgumentException, SecurityException, IOException
/*/*from w w w.j a v a2s .c o m*/ Copyright ? 2008 Brent Boyer This program 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. This program 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 Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project). If not, see <http://www.gnu.org/licenses/>. */ import bb.science.FormatUtil; import bb.util.Check; import bb.util.StringUtil; import bb.util.ThrowableUtil; import bb.util.logging.LogUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.util.Random; import java.util.logging.Level; import org.junit.Assert; import org.junit.Test; public class Main{ /** * Calls <code>{@link #delete delete}(file)</code> if file != null and file exists. * <p> * Note: delete will do some additional tests of file (that it is a normal file that can be read by this application). * <p> * Note: {@link DirUtil#deleteIfExists DirUtil.deleteIfExists} should be used to delete directories if they exist. * <p> * @param file a normal file that is to be deleted; may be null or non-existent * @throws IllegalArgumentException if file is not a normal file * @throws SecurityException if a security manager exists and denies read access to the file * @throws IOException if an I/O problem occurs; this includes failure of the file to be deleted */ public static void deleteIfExists(File file) throws IllegalArgumentException, SecurityException, IOException { if ((file != null) && file.exists()) delete(file); } /** * Trys to delete file, and throws an IOException if the delete failed. * It is useful because {@link File#delete File.delete} unfortunately merely returns a boolean indicating * the success of the operation, rather than throwing an Exception. * <p> * Note: {@link DirUtil#delete DirUtil.delete} should be used to delete directories. * <p> * @param file a normal file that is to be deleted * @throws IllegalArgumentException if file is {@link Check#validFile not valid} * @throws SecurityException if a security manager exists and denies read access to the file * @throws IOException if an I/O problem occurs; this includes failure of the file to be deleted */ public static void delete(File file) throws IllegalArgumentException, SecurityException, IOException { Check.arg().validFile(file); boolean deleted = file.delete(); if (!deleted) throw new IOException("Failed to delete " + file.getPath()); } }