org.apache.qpid.util
Class FileUtils

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

public class FileUtils
extends Object

FileUtils provides some simple helper methods for working with files. It follows the convention of wrapping all checked exceptions as runtimes, so code using these methods is free of try-catch blocks but does not expect to recover from errors.

CRC Card
Responsibilities Collaborations
Read a text file as a string.
Open a file or default resource as an input stream.


Nested Class Summary
static class FileUtils.UnableToCopyException
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copy(File src, File dst)
          Copies the specified source file to the specified destintaion file.
static void copyCheckedEx(File src, File dst)
          Copies the specified source file to the specified destination file.
static void copyRecursive(File source, File dst)
           
static boolean delete(File file, boolean recursive)
          Delete a given file/directory, A directory will always require the recursive flag to be set.
static boolean deleteDirectory(String directoryPath)
           
static boolean deleteFile(String filePath)
           
static InputStream openFileOrDefaultResource(String filename, String defaultResource, ClassLoader cl)
          Either opens the specified filename as an input stream, or uses the default resource loaded using the specified class loader, if opening the file fails or no file name is specified.
static String readFileAsString(File file)
          Reads a text file as a string.
static String readFileAsString(String filename)
          Reads a text file as a string.
static List<String> searchFile(File file, String search)
          Checks the specified file for instances of the search string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

readFileAsString

public static String readFileAsString(String filename)
Reads a text file as a string.

Parameters:
filename - The name of the file.
Returns:
The contents of the file.

readFileAsString

public static String readFileAsString(File file)
Reads a text file as a string.

Parameters:
file - The file.
Returns:
The contents of the file.

openFileOrDefaultResource

public static InputStream openFileOrDefaultResource(String filename,
                                                    String defaultResource,
                                                    ClassLoader cl)
Either opens the specified filename as an input stream, or uses the default resource loaded using the specified class loader, if opening the file fails or no file name is specified.

Parameters:
filename - The name of the file to open.
defaultResource - The name of the default resource on the classpath if the file cannot be opened.
cl - The classloader to load the default resource with.
Returns:
An input stream for the file or resource, or null if one could not be opened.

copy

public static void copy(File src,
                        File dst)
Copies the specified source file to the specified destintaion file. If the destinationst file does not exist, it is created.

Parameters:
src - The source file name.
dst - The destination file name.

copyCheckedEx

public static void copyCheckedEx(File src,
                                 File dst)
                          throws IOException
Copies the specified source file to the specified destination file. If the destination file does not exist, it is created.

Parameters:
src - The source file name.
dst - The destination file name.
Throws:
IOException

deleteFile

public static boolean deleteFile(String filePath)

deleteDirectory

public static boolean deleteDirectory(String directoryPath)

delete

public static boolean delete(File file,
                             boolean recursive)
Delete a given file/directory, A directory will always require the recursive flag to be set. if a directory is specified and recursive set then delete the whole tree

Parameters:
file - the File object to start at
recursive - boolean to recurse if a directory is specified.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise

copyRecursive

public static void copyRecursive(File source,
                                 File dst)
                          throws FileNotFoundException,
                                 FileUtils.UnableToCopyException
Throws:
FileNotFoundException
FileUtils.UnableToCopyException

searchFile

public static List<String> searchFile(File file,
                                      String search)
                               throws IOException
Checks the specified file for instances of the search string.

Parameters:
file - the file to search
search - the search String
Returns:
the list of matching entries
Throws:
IOException


Licensed to the Apache Software Foundation