FileDeleteStrategy.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io;

  18. import java.io.File;
  19. import java.io.IOException;

  20. /**
  21.  * Strategy for deleting files.
  22.  * <p>
  23.  * There is more than one way to delete a file.
  24.  * You may want to limit access to certain directories, to only delete
  25.  * directories if they are empty, or maybe to force deletion.
  26.  * </p>
  27.  * <p>
  28.  * This class captures the strategy to use and is designed for user subclassing.
  29.  * </p>
  30.  *
  31.  * @since 1.3
  32.  */
  33. public class FileDeleteStrategy {

  34.     /**
  35.      * Force file deletion strategy.
  36.      */
  37.     static class ForceFileDeleteStrategy extends FileDeleteStrategy {

  38.         /** Default Constructor */
  39.         ForceFileDeleteStrategy() {
  40.             super("Force");
  41.         }

  42.         /**
  43.          * Deletes the file object.
  44.          * <p>
  45.          * This implementation uses {@code FileUtils.forceDelete()}
  46.          * if the file exists.
  47.          * </p>
  48.          *
  49.          * @param fileToDelete  the file to delete, not null
  50.          * @return Always returns {@code true}
  51.          * @throws NullPointerException if the file is null
  52.          * @throws IOException if an error occurs during file deletion
  53.          */
  54.         @Override
  55.         protected boolean doDelete(final File fileToDelete) throws IOException {
  56.             FileUtils.forceDelete(fileToDelete);
  57.             return true;
  58.         }
  59.     }

  60.     /**
  61.      * The singleton instance for normal file deletion, which does not permit
  62.      * the deletion of directories that are not empty.
  63.      */
  64.     public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal");

  65.     /**
  66.      * The singleton instance for forced file deletion, which always deletes,
  67.      * even if the file represents a non-empty directory.
  68.      */
  69.     public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();

  70.     /** The name of the strategy. */
  71.     private final String name;

  72.     /**
  73.      * Restricted constructor.
  74.      *
  75.      * @param name  the name by which the strategy is known
  76.      */
  77.     protected FileDeleteStrategy(final String name) {
  78.         this.name = name;
  79.     }

  80.     /**
  81.      * Deletes the file object, which may be a file or a directory.
  82.      * If the file does not exist, the method just returns.
  83.      * <p>
  84.      * Subclass writers should override {@link #doDelete(File)}, not this method.
  85.      * </p>
  86.      *
  87.      * @param fileToDelete  the file to delete, not null
  88.      * @throws NullPointerException if the file is null
  89.      * @throws IOException if an error occurs during file deletion
  90.      */
  91.     public void delete(final File fileToDelete) throws IOException {
  92.         if (fileToDelete.exists() && !doDelete(fileToDelete)) {
  93.             throw new IOException("Deletion failed: " + fileToDelete);
  94.         }
  95.     }

  96.     /**
  97.      * Deletes the file object, which may be a file or a directory.
  98.      * All {@link IOException}s are caught and false returned instead.
  99.      * If the file does not exist or is null, true is returned.
  100.      * <p>
  101.      * Subclass writers should override {@link #doDelete(File)}, not this method.
  102.      * </p>
  103.      *
  104.      * @param fileToDelete  the file to delete, null returns true
  105.      * @return true if the file was deleted, or there was no such file
  106.      */
  107.     public boolean deleteQuietly(final File fileToDelete) {
  108.         if (fileToDelete == null || !fileToDelete.exists()) {
  109.             return true;
  110.         }
  111.         try {
  112.             return doDelete(fileToDelete);
  113.         } catch (final IOException ex) {
  114.             return false;
  115.         }
  116.     }

  117.     /**
  118.      * Actually deletes the file object, which may be a file or a directory.
  119.      * <p>
  120.      * This method is designed for subclasses to override.
  121.      * The implementation may return either false or an {@link IOException}
  122.      * when deletion fails. The {@link #delete(File)} and {@link #deleteQuietly(File)}
  123.      * methods will handle either response appropriately.
  124.      * A check has been made to ensure that the file will exist.
  125.      * </p>
  126.      * <p>
  127.      * This implementation uses {@link FileUtils#delete(File)}.
  128.      * </p>
  129.      *
  130.      * @param file  the file to delete, exists, not null
  131.      * @return true if the file was deleted
  132.      * @throws NullPointerException if the file is null
  133.      * @throws IOException if an error occurs during file deletion
  134.      */
  135.     protected boolean doDelete(final File file) throws IOException {
  136.         FileUtils.delete(file);
  137.         return true;
  138.     }

  139.     /**
  140.      * Gets a string describing the delete strategy.
  141.      *
  142.      * @return a string describing the delete strategy
  143.      */
  144.     @Override
  145.     public String toString() {
  146.         return "FileDeleteStrategy[" + name + "]";
  147.     }

  148. }