Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.
  */
 package org.apache.tomcat.util.http.fileupload;
 
 import static org.jboss.web.FileUploadMessages.MESSAGES;
 
 import java.io.File;
Strategy for deleting files.

There is more than one way to delete a file. You may want to limit access to certain directories, to only delete directories if they are empty, or maybe to force deletion.

This class captures the strategy to use and is designed for user subclassing.

Author(s):
Stephen Colebourne
Version:
$Id: FileDeleteStrategy.java 881533 2009-11-17 21:21:41Z markt $
Since:
Commons IO 1.3
 
 public class FileDeleteStrategy {

    
The singleton instance for normal file deletion, which does not permit the deletion of directories that are not empty.
 
     public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal");
    
The singleton instance for forced file deletion, which always deletes, even if the file represents a non-empty directory.
 
     public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();

    
The name of the strategy.
 
     private final String name;
 
     //-----------------------------------------------------------------------
     
Restricted constructor.

Parameters:
name the name by which the strategy is known
 
     protected FileDeleteStrategy(String name) {
         this. = name;
     }
 
     //-----------------------------------------------------------------------
     
Deletes the file object, which may be a file or a directory. All IOExceptions are caught and false returned instead. If the file does not exist or is null, true is returned.

Subclass writers should override doDelete(java.io.File), not this method.

Parameters:
fileToDelete the file to delete, null returns true
Returns:
true if the file was deleted, or there was no such file
 
     public boolean deleteQuietly(File fileToDelete) {
         if (fileToDelete == null || fileToDelete.exists() == false) {
             return true;
         }
         try {
             return doDelete(fileToDelete);
         } catch (IOException ex) {
             return false;
         }
     }

    
Deletes the file object, which may be a file or a directory. If the file does not exist, the method just returns.

Subclass writers should override doDelete(java.io.File), not this method.

Parameters:
fileToDelete the file to delete, not null
Throws:
java.lang.NullPointerException if the file is null
java.io.IOException if an error occurs during file deletion
 
     public void delete(File fileToDeletethrows IOException {
         if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
             throw new IOException(.failedToDelete(fileToDelete.getAbsolutePath()));
         }
     }

    
Actually deletes the file object, which may be a file or a directory.

This method is designed for subclasses to override. The implementation may return either false or an IOException when deletion fails. The delete(java.io.File) and deleteQuietly(java.io.File) methods will handle either response appropriately. A check has been made to ensure that the file will exist.

This implementation uses java.io.File.delete().

Parameters:
fileToDelete the file to delete, exists, not null
Returns:
true if the file was deleteds
Throws:
java.lang.NullPointerException if the file is null
java.io.IOException if an error occurs during file deletion
    protected boolean doDelete(File fileToDeletethrows IOException {
        return fileToDelete.delete();
    }
    //-----------------------------------------------------------------------
    
Gets a string describing the delete strategy.

Returns:
a string describing the delete strategy
    @Override
    public String toString() {
        return "FileDeleteStrategy[" +  + "]";
    }
    //-----------------------------------------------------------------------
    
Force file deletion strategy.
    static class ForceFileDeleteStrategy extends FileDeleteStrategy {
        
Default Constructor
        ForceFileDeleteStrategy() {
            super("Force");
        }

        
Deletes the file object.

This implementation uses FileUtils.forceDelete() if the file exists.

Parameters:
fileToDelete the file to delete, not null
Returns:
Always returns true
Throws:
java.lang.NullPointerException if the file is null
java.io.IOException if an error occurs during file deletion
        @Override
        protected boolean doDelete(File fileToDeletethrows IOException {
            FileUtils.forceDelete(fileToDelete);
            return true;
        }
    }
New to GrepCode? Check out our FAQ X