Here you can find the source of deleteFilesIfExist(Path... files)
Parameter | Description |
---|---|
files | files to delete |
public static void deleteFilesIfExist(Path... files) throws IOException
//package com.java2s; /*// w w w . j a v a 2s . c om * 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.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; public class Main { /** * Deletes all given <tt>Path</tt>s, if they exist. Some of the * <tt>File</tt>s may be null; they are * ignored. After everything is deleted, the method either * throws the first exception it hit while deleting, or * completes normally if there were no exceptions. * * @param files files to delete */ public static void deleteFilesIfExist(Path... files) throws IOException { deleteFilesIfExist(Arrays.asList(files)); } /** * Deletes all given <tt>Path</tt>s, if they exist. Some of the * <tt>File</tt>s may be null; they are * ignored. After everything is deleted, the method either * throws the first exception it hit while deleting, or * completes normally if there were no exceptions. * * @param files files to delete */ public static void deleteFilesIfExist(Collection<? extends Path> files) throws IOException { Throwable th = null; for (Path file : files) { try { if (file != null) { Files.deleteIfExists(file); } } catch (Throwable t) { addSuppressed(th, t); if (th == null) { th = t; } } } reThrow(th); } /** adds a Throwable to the list of suppressed Exceptions of the first Throwable * @param exception this exception should get the suppressed one added * @param suppressed the suppressed exception */ private static void addSuppressed(Throwable exception, Throwable suppressed) { if (exception != null && suppressed != null) { exception.addSuppressed(suppressed); } } /** * Simple utility method that takes a previously caught * {@code Throwable} and rethrows either {@code * IOException} or an unchecked exception. If the * argument is null then this method does nothing. */ public static void reThrow(Throwable th) throws IOException { if (th != null) { if (th instanceof IOException) { throw (IOException) th; } reThrowUnchecked(th); } } /** * Simple utility method that takes a previously caught * {@code Throwable} and rethrows it as an unchecked exception. * If the argument is null then this method does nothing. */ public static void reThrowUnchecked(Throwable th) { if (th != null) { if (th instanceof RuntimeException) { throw (RuntimeException) th; } if (th instanceof Error) { throw (Error) th; } throw new RuntimeException(th); } } }