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;
General file manipulation utilities.

Facilities are provided in the following areas:

  • writing to a file
  • reading from a file
  • make a directory including parent directories
  • copying files and directories
  • deleting files and directories
  • converting to and from a URL
  • listing files and directories by filter and extension
  • comparing file content
  • file last changed date
  • calculating a checksum

Origin of code: Excalibur, Alexandria, Commons-Utils

Author(s):
Kevin A. Burton
Scott Sanders
Daniel Rall
Christoph.Reck
Peter Donald
Jeff Turner
Matthew Hawthorne
Jeremias Maerki
Stephen Colebourne
Ian Springer
Chris Eldredge
Jim Harrington
Niall Pemberton
Sandy McArthur
Version:
$Id: FileUtils.java 881493 2009-11-17 20:30:39Z markt $
 
 public class FileUtils {

    
Instances should NOT be constructed in standard programming.
 
     public FileUtils() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Deletes a directory recursively.

Parameters:
directory directory to delete
Throws:
java.io.IOException in case deletion is unsuccessful
 
     public static void deleteDirectory(File directorythrows IOException {
         if (!directory.exists()) {
             return;
         }
 
         cleanDirectory(directory);
         if (!directory.delete()) {
             throw new IOException(.failedToDeleteDirectory(directory.getPath()));
         }
     }

    

    
Cleans a directory without deleting it.

Parameters:
directory directory to clean
Throws:
java.io.IOException in case cleaning is unsuccessful
 
     public static void cleanDirectory(File directorythrows IOException {
         if (!directory.exists()) {
             throw .missingDirectory(directory.getPath());
         }
        if (!directory.isDirectory()) {
            throw .notDirectory(directory.getPath());
        }
        File[] files = directory.listFiles();
        if (files == null) {  // null if security restricted
            throw new IOException(.failedListingDirectory(directory.getPath()));
        }
        IOException exception = null;
        for (int i = 0; i < files.lengthi++) {
            File file = files[i];
            try {
                forceDelete(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }
        if (null != exception) {
            throw exception;
        }
    }
    
    
    //-----------------------------------------------------------------------
    
Deletes a file. If file is a directory, delete it and all sub-directories.

The difference between File.delete() and this method are:

  • A directory to be deleted does not have to be empty.
  • You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)

Parameters:
file file or directory to delete, must not be null
Throws:
java.lang.NullPointerException if the directory is null
java.io.FileNotFoundException if the file was not found
java.io.IOException in case deletion is unsuccessful
    public static void forceDelete(File filethrows IOException {
        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            boolean filePresent = file.exists();
            if (!file.delete()) {
                if (!filePresent){
                    throw new FileNotFoundException(.missingFile(file.getPath()));
                }
                throw new IOException(.failedToDelete(file.getPath()));
            }
        }
    }

    
Schedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.

Parameters:
file file or directory to delete, must not be null
Throws:
java.lang.NullPointerException if the file is null
java.io.IOException in case deletion is unsuccessful
    public static void forceDeleteOnExit(File filethrows IOException {
        if (file.isDirectory()) {
            deleteDirectoryOnExit(file);
        } else {
            file.deleteOnExit();
        }
    }

    
Schedules a directory recursively for deletion on JVM exit.

Parameters:
directory directory to delete, must not be null
Throws:
java.lang.NullPointerException if the directory is null
java.io.IOException in case deletion is unsuccessful
    private static void deleteDirectoryOnExit(File directorythrows IOException {
        if (!directory.exists()) {
            return;
        }
        cleanDirectoryOnExit(directory);
        directory.deleteOnExit();
    }

    
Cleans a directory without deleting it.

Parameters:
directory directory to clean, must not be null
Throws:
java.lang.NullPointerException if the directory is null
java.io.IOException in case cleaning is unsuccessful
    private static void cleanDirectoryOnExit(File directorythrows IOException {
        if (!directory.exists()) {
            throw .missingDirectory(directory.getPath());
        }
        if (!directory.isDirectory()) {
            throw .notDirectory(directory.getPath());
        }
        File[] files = directory.listFiles();
        if (files == null) {  // null if security restricted
            throw new IOException(.failedListingDirectory(directory.getPath()));
        }
        IOException exception = null;
        for (int i = 0; i < files.lengthi++) {
            File file = files[i];
            try {
                forceDeleteOnExit(file);
            } catch (IOException ioe) {
                exception = ioe;
            }
        }
        if (null != exception) {
            throw exception;
        }
    }
New to GrepCode? Check out our FAQ X