Package org.apache.qpid.server.util
Class FileUtils
- java.lang.Object
-
- org.apache.qpid.server.util.FileUtils
-
public class FileUtils extends java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileUtils.UnableToCopyException
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
copy(java.io.File src, java.io.File dst)
Copies the specified source file to the specified destintaion file.static void
copy(java.io.InputStream in, java.io.File dst)
Copies the specified InputStream to the specified destination file.static void
copyCheckedEx(java.io.File src, java.io.File dst)
Copies the specified source file to the specified destination file.static void
copyRecursive(java.io.File source, java.io.File dst)
static boolean
delete(java.io.File file, boolean recursive)
Delete a given file/directory, A directory will always require the recursive flag to be set.static boolean
deleteDirectory(java.lang.String directoryPath)
static boolean
deleteFile(java.lang.String filePath)
static java.io.InputStream
openFileOrDefaultResource(java.lang.String filename, java.lang.String defaultResource, java.lang.ClassLoader cl)
Either opens the specified filename as an input stream or either the filesystem or classpath, or uses the default resource loaded using the specified class loader, if opening the file fails or no file name is specified.static byte[]
readFileAsBytes(java.lang.String filename)
Reads a text file as a string.static java.lang.String
readFileAsString(java.io.File file)
Reads a text file as a string.static java.lang.String
readFileAsString(java.lang.String filename)
Reads a text file as a string.static java.util.List<java.lang.String>
searchFile(java.io.File file, java.lang.String search)
Checks the specified file for instances of the search string.
-
-
-
Method Detail
-
readFileAsBytes
public static byte[] readFileAsBytes(java.lang.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 java.lang.String readFileAsString(java.lang.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 java.lang.String readFileAsString(java.io.File file)
Reads a text file as a string.- Parameters:
file
- The file.- Returns:
- The contents of the file.
-
openFileOrDefaultResource
public static java.io.InputStream openFileOrDefaultResource(java.lang.String filename, java.lang.String defaultResource, java.lang.ClassLoader cl)
Either opens the specified filename as an input stream or either the filesystem or classpath, 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(java.io.File src, java.io.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(java.io.File src, java.io.File dst) throws java.io.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:
java.io.IOException
- if there is an issue copying the file
-
copy
public static void copy(java.io.InputStream in, java.io.File dst) throws java.io.IOException
Copies the specified InputStream to the specified destination file. If the destination file does not exist, it is created.- Parameters:
in
- The InputStreamdst
- The destination file name.- Throws:
java.io.IOException
- if there is an issue copying the stream
-
deleteFile
public static boolean deleteFile(java.lang.String filePath)
-
deleteDirectory
public static boolean deleteDirectory(java.lang.String directoryPath)
-
delete
public static boolean delete(java.io.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 atrecursive
- 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(java.io.File source, java.io.File dst) throws java.io.FileNotFoundException, FileUtils.UnableToCopyException
- Throws:
java.io.FileNotFoundException
FileUtils.UnableToCopyException
-
searchFile
public static java.util.List<java.lang.String> searchFile(java.io.File file, java.lang.String search) throws java.io.IOException
Checks the specified file for instances of the search string.- Parameters:
file
- the file to searchsearch
- the search String- Returns:
- the list of matching entries
- Throws:
java.io.IOException
- if there is an issue searching the file
-
-