org.apache.muse.util
Class FileUtils

java.lang.Object
  extended by org.apache.muse.util.FileUtils

public final class FileUtils
extends Object

FileUtils is a collection of routines for common file system operations.

Author:
Dan Jemiolo (danj)

Field Summary
static File CURRENT_DIR
          The application's current working directory.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void close(InputStream stream)
          This is a convenience method that throws an unchecked exception if the stream could not be closed.
static void close(OutputStream stream)
          This is a convenience method that throws an unchecked exception if the stream could not be closed.
static void close(Reader reader)
          This is a convenience method that throws an unchecked exception if the reader could not be closed.
static void close(Writer writer)
          This is a convenience method that throws an unchecked exception if the writer could not be closed.
static void copy(File source, File destination)
          Copies the first file or directory to the second file or directory.
static void copyDirectory(File source, File destination)
           
static void copyDirectory(File source, File destination, FileFilter filter)
           
static void copyFile(File source, File destination)
           
static void copyFile(InputStream input, File destination)
           
static String createRelativePath(String originalPath, String relativePath)
          Merges the two paths to create a valid version of the second path.
static File find(File contextRoot, String fileName)
          This is a convenience method that calls find(File, String, boolean) with the last parameter set to "false" (does not match directories).
static File find(File contextRoot, String fileName, boolean matchDirectories)
          Searches through the directory tree under the given context directory and finds the first file that matches the file name.
static InputStream loadFromContext(Class context, String path)
          Gets a java.io.File using a path that is relative to the context of the given Class.
static String makePath(String[] strings)
          Convert a list of path elements to a platform-specific path.
static void pruneEmptyDirectories(File directory)
          Starts at the directory given and tests to see whether it is empty; if so, it deletes it and moves up the directory tree, deleting empty directories until it finds a non-empty one.
static void remove(File file)
          This is a convenience method that calls remove(File, boolean) with the second parameter set to "false" (doesn't prune empty directories).
static void remove(File file, boolean pruneEmptyDirectories)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CURRENT_DIR

public static final File CURRENT_DIR
The application's current working directory.

Constructor Detail

FileUtils

public FileUtils()
Method Detail

close

public static void close(InputStream stream)
This is a convenience method that throws an unchecked exception if the stream could not be closed.


close

public static void close(OutputStream stream)
This is a convenience method that throws an unchecked exception if the stream could not be closed.


close

public static void close(Reader reader)
This is a convenience method that throws an unchecked exception if the reader could not be closed.


close

public static void close(Writer writer)
This is a convenience method that throws an unchecked exception if the writer could not be closed.


copy

public static void copy(File source,
                        File destination)
                 throws IOException
Copies the first file or directory to the second file or directory.

If the first parameter is a file and the second is a file, then the method copies the contents of the first file into the second. If the second file does not exist, it is created.

If the first parameter is a file and the second is a directory, the file is copied to the directory, overwriting any existing copy.

If the first parameter is a directory and the second is a directory, the first is copied underneath the second.

If the first parameter is a directory and the second is a file name or does not exist, a directory with that name is created, and the contents of the first directory are copied there.

Parameters:
source -
destination -
Throws:
IOException -
  • If the source does not exist.
  • If the user does not have permission to modify the destination.
  • If the copy fails for some reason related to system I/O.

copyDirectory

public static void copyDirectory(File source,
                                 File destination)
                          throws IOException
Throws:
IOException

copyDirectory

public static void copyDirectory(File source,
                                 File destination,
                                 FileFilter filter)
                          throws IOException
Throws:
IOException

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
Throws:
IOException

copyFile

public static void copyFile(InputStream input,
                            File destination)
                     throws IOException
Throws:
IOException

createRelativePath

public static String createRelativePath(String originalPath,
                                        String relativePath)
Merges the two paths to create a valid version of the second path. This method should be used when you encounter a relative path in a document and must resolve it based on the path of the current document. An example would be:

original path - files/customers/Orders.xml

relative path - ../Accounts.xml

result - files/customers/Accounts.xml

The only time this method cannot be used is if the original path is for a file that is in the root (has no directory as part of its path) and the relative path starts with "..".

Parameters:
originalPath - The path of the file that references another file.
relativePath - The path of the other file, which is relative to the original.
Returns:
A proper path for the other file, one that can be used to open and verify the file.

find

public static File find(File contextRoot,
                        String fileName)
This is a convenience method that calls find(File, String, boolean) with the last parameter set to "false" (does not match directories).

See Also:
find(File, String, boolean)

find

public static File find(File contextRoot,
                        String fileName,
                        boolean matchDirectories)
Searches through the directory tree under the given context directory and finds the first file that matches the file name. If the third parameter is true, the method will also try to match directories, not just "regular" files.

Parameters:
contextRoot - The directory to start the search from.
fileName - The name of the file (or directory) to search for.
matchDirectories - True if the method should try and match the name against directory names, not just file names.
Returns:
The java.io.File representing the first file or directory with the given name, or null if it was not found.

loadFromContext

public static InputStream loadFromContext(Class context,
                                          String path)
Gets a java.io.File using a path that is relative to the context of the given Class. This uses the Class.getResource() method.

Parameters:
context - The classpath from which to load.
path - The context-relative file path.
Returns:
A java.io.File representing the specified path or null if file could not be found.

makePath

public static String makePath(String[] strings)
Convert a list of path elements to a platform-specific path.

Parameters:
strings - Elements in a path
Returns:
an absolute path using the current platform's File.separator

pruneEmptyDirectories

public static void pruneEmptyDirectories(File directory)
                                  throws IOException
Starts at the directory given and tests to see whether it is empty; if so, it deletes it and moves up the directory tree, deleting empty directories until it finds a non-empty one.

Parameters:
directory - The first directory to test.
Throws:
IOException -
  • If the directory does not exist or the user does not have permission to delete it or its parents.

remove

public static void remove(File file)
                   throws IOException
This is a convenience method that calls remove(File, boolean) with the second parameter set to "false" (doesn't prune empty directories).

Throws:
IOException
See Also:
remove(File, boolean)

remove

public static void remove(File file,
                          boolean pruneEmptyDirectories)
                   throws IOException
Parameters:
file - The file or directory to delete.
pruneEmptyDirectories - True if the deletion results in an empty parent directory. If set to true, this method will traverse up the directory tree, deleting directories that are made empty by the deletion.
Throws:
IOException -
  • If there was an error trying to remove the file or directory. The file system may be in an inconsistent state when the exception is thrown (directories may be partially deleted, etc.).


Copyright © 2005-2011 Apache Web Services - Muse. All Rights Reserved.