Package rife.tools
Class FileUtils
java.lang.Object
rife.tools.FileUtils
A utility class for handling files.
- Since:
- 1.0
- 
Field SummaryFields
- 
Method SummaryModifier and TypeMethodDescriptioncombineToAbsolutePaths(List<File>... files) Combines an arbitrary number of lists of files into a single list of absolute file paths.static voidCopy the contents of one file to another file.static voidcopy(File source, OutputStream outputStream) Copy the contents of a file to an OutputStream.static voidcopy(InputStream inputStream, File target) Copy the contents of an InputStream to a file.static voidcopy(InputStream inputStream, OutputStream outputStream) Copy the contents of an InputStream to an OutputStream.static voidcopyDirectory(File sourceDir, File targetDir) Copies all files and directories from the source directory to the target directory.static voiddeleteDirectory(File source) Deletes a directory and all its contents recursively.static voiddeleteFile(File file) Deletes a given file.static StringgenerateDirectoryListing(File directory) Generates a sorted directory listing of all files and subdirectories in the specified directory.static StringgetBaseName(File file) Returns the base name of the specified file, without the extension.static StringgetBaseName(String fileName) Returns the base name of the specified file name, without the extension.static StringgetExtension(File file) Returns the extension of the specifiedFile.static StringgetExtension(String fileName) Returns the extension of the specified file name.getFileList(File dir) Returns a list of files in the given directory.Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.getFileList(File dir, Pattern[] included, Pattern[] excluded) Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.getFileList(File dir, Pattern included, Pattern excluded) Returns a list of files in the given directory that match the specified inclusion and exclusion patterns.getJavaFileList(File directory) Returns a list of Java files in the specified directory.static StringGenerates a unique filename.static StringJoins a list of file paths into a single string with the file path separator.static voidmoveDirectory(File source, File target) Moves the source directory to the target location.static voidMoves the source file to the target location.static PathConstructs a newPathinstance using the specified file and additional path elements.static PathConstructs a newPathinstance using the specified file and additional path elements.static Set<PosixFilePermission>permissionsFromMode(int mode) Creates a new set ofPosixFilePermissionbased on the given Posix mode.static byte[]Reads all the bytes from theURL.static byte[]readBytes(InputStream inputStream) Reads all the bytes from theInputStream.static byte[]Reads all the bytes from the file denoted by the specifiedFileobject.static ByteArrayOutputStreamreadStream(InputStream inputStream) Reads the complete contents of an input stream and returns it as a ByteArrayOutputStream object.static StringreadString(File source) Reads the content of the given file into a string using the default encoding.static StringreadString(File source, String encoding) Reads the content of the given file into a string using the specified character encoding.static StringreadString(InputStream inputStream) Reads the entire contents of an InputStream and returns it as a String, using UTF-8 encoding.static StringreadString(InputStream inputStream, String encoding) Reads the entire contents of an InputStream and returns it as a String, using the specified character encoding.static StringreadString(Reader reader) Reads the entire contents of a Reader and returns it as a String.static StringreadString(URL source) Reads all characters from theURLand returns its contents as a string.static StringreadString(URL source, String encoding) Reads the content of the given URL into a string.static voidtransformFiles(File directory, Function<String, String> transformer) Transforms all files in the specified directory using the provided transformation function.static voidtransformFiles(File directory, List<Pattern> included, List<Pattern> excluded, Function<String, String> transformer) Transforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.static voidtransformFiles(File directory, Pattern[] included, Pattern[] excluded, Function<String, String> transformer) Transforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.static voidtransformFiles(File directory, Pattern included, Pattern excluded, Function<String, String> transformer) Transforms files in the specified directory that match the included pattern and do not match the excluded pattern using the provided transformation function.static voidUnzips a file from the source location to a destination.static StringversionFromResource(String name) Reads the contents of a resource for use as a version number.static voidwriteBytes(byte[] content, File destination) Writes a byte array to a given file.static voidwriteString(String content, File destination) Writes a string to a given file.
- 
Field Details- 
JAVA_FILE_PATTERN
- 
JAR_FILE_PATTERN
 
- 
- 
Method Details- 
getFileListReturns a list of files in the given directory.- Parameters:
- dir- The directory to search for files.
- Returns:
- A list of files in the given directory.
- Since:
- 1.0
 
- 
getFileListReturns a list of files in the given directory that match the specified inclusion and exclusion patterns.- Parameters:
- dir- The directory to search for files.
- included- A pattern for files to include.
- excluded- A pattern for files to exclude.
- Returns:
- A list of files in the given directory that match the specified inclusion and exclusion patterns.
- Since:
- 1.0
 
- 
getFileListReturns a list of files in the given directory that match the specified inclusion and exclusion patterns.- Parameters:
- dir- The directory to search for files.
- included- A list of patterns for files to include.
- excluded- A list of patterns for files to exclude.
- Returns:
- A list of files in the given directory that match the specified inclusion and exclusion patterns.
- Since:
- 1.0
 
- 
getFileListReturns a list of files in the given directory that match the specified inclusion and exclusion patterns.- Parameters:
- dir- The directory to search for files.
- included- An array of patterns for files to include.
- excluded- An array of patterns for files to exclude.
- Returns:
- A list of files in the given directory that match the specified inclusion and exclusion patterns.
- Since:
- 1.0
 
- 
moveFileMoves the source file to the target location.- Parameters:
- source- the file to be moved.
- target- the new location to move the file to.
- Throws:
- FileUtilsErrorException- if an error occurs while moving the file.
- IllegalArgumentException- if either the source or target is null.
- Since:
- 1.0
 
- 
moveDirectoryMoves the source directory to the target location.- Parameters:
- source- the directory to be moved.
- target- the new location to move the directory to.
- Throws:
- FileUtilsErrorException- if an error occurs while moving the directory.
- IllegalArgumentException- if either the source or target is null.
- Since:
- 1.0
 
- 
deleteDirectoryDeletes a directory and all its contents recursively.- Parameters:
- source- the directory to be deleted.
- Throws:
- FileUtilsErrorException- if an error occurs while deleting the directory.
- IllegalArgumentException- if the source is null.
- Since:
- 1.0
 
- 
copypublic static void copy(InputStream inputStream, OutputStream outputStream) throws FileUtilsErrorException Copy the contents of an InputStream to an OutputStream.- Parameters:
- inputStream- the InputStream to copy from. Must not be null.
- outputStream- the OutputStream to copy to. Must not be null.
- Throws:
- FileUtilsErrorException- if there is an error during the copying of streams.
- IllegalArgumentException- if either inputStream or outputStream is null.
- Since:
- 1.0
 
- 
copyCopy the contents of an InputStream to a file.- Parameters:
- inputStream- the InputStream to copy from; must not be null
- target- the File to copy to; must not be null
- Throws:
- FileUtilsErrorException- if an error occurs while copying the stream to the file
- IllegalArgumentException- if inputStream or target is null
- Since:
- 1.0
 
- 
copyCopy the contents of a file to an OutputStream.- Parameters:
- source- the File to copy from; must not be null
- outputStream- the OutputStream to copy to; must not be null
- Throws:
- FileUtilsErrorException- if an error occurs while copying the file to the stream
- IllegalArgumentException- if source or outputStream is null
- Since:
- 1.0
 
- 
copyCopy the contents of one file to another file.- Parameters:
- source- the File to copy from; must not be null
- target- the File to copy to; must not be null
- Throws:
- FileUtilsErrorException- if an error occurs while copying the source file to the target file
- IllegalArgumentException- if source or target is null
- Since:
- 1.0
 
- 
copyDirectoryCopies all files and directories from the source directory to the target directory.- Parameters:
- sourceDir- the directory to be copied from
- targetDir- the directory to be copied to
- Throws:
- FileUtilsErrorException- if there's an error copying the files
- IllegalArgumentException- if either sourceDir or targetDir are null
- Since:
- 1.0
 
- 
readStreampublic static ByteArrayOutputStream readStream(InputStream inputStream) throws FileUtilsErrorException Reads the complete contents of an input stream and returns it as a ByteArrayOutputStream object.- Parameters:
- inputStream- the input stream to be read.
- Returns:
- a ByteArrayOutputStream object containing the complete contents of the specified input stream.
- Throws:
- FileUtilsErrorException- if there is an error while reading the input stream.
- IllegalArgumentException- if the inputStream parameter is null.
- Since:
- 1.0
 
- 
readStringReads the entire contents of an InputStream and returns it as a String, using UTF-8 encoding.- Parameters:
- inputStream- The InputStream to read the contents from.
- Returns:
- A String containing the complete contents of the InputStream.
- Throws:
- FileUtilsErrorException- If there was an error while reading the InputStream.
- IllegalArgumentException- If the inputStream is null.
- Since:
- 1.0
 
- 
readStringReads the entire contents of a Reader and returns it as a String.- Parameters:
- reader- The Reader to read the contents from.
- Returns:
- A String containing the complete contents of the Reader.
- Throws:
- FileUtilsErrorException- If there was an error while reading the Reader.
- IllegalArgumentException- If the reader is null.
- Since:
- 1.0
 
- 
readStringpublic static String readString(InputStream inputStream, String encoding) throws FileUtilsErrorException Reads the entire contents of an InputStream and returns it as a String, using the specified character encoding.- Parameters:
- inputStream- The InputStream to read the contents from.
- encoding- The name of the character encoding to use.
- Returns:
- A String containing the complete contents of the InputStream.
- Throws:
- FileUtilsErrorException- If there was an error while reading the InputStream.
- IllegalArgumentException- If the inputStream is null.
- Since:
- 1.0
 
- 
readStringReads the content of the given URL into a string.- Parameters:
- source- the URL to read from
- encoding- the character encoding to use, or null to use the default encoding
- Returns:
- the content of the URL as a string
- Throws:
- FileUtilsErrorException- if an error occurs while reading the URL
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
readStringReads the content of the given file into a string using the default encoding.- Parameters:
- source- the file to read from
- Returns:
- the content of the file as a string
- Throws:
- FileUtilsErrorException- if an error occurs while reading the file
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
readStringReads the content of the given file into a string using the specified character encoding.- Parameters:
- source- the file to read from
- encoding- the character encoding to use, or null to use the default encoding
- Returns:
- the content of the file as a string
- Throws:
- FileUtilsErrorException- if an error occurs while reading the file
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
readStringReads all characters from theURLand returns its contents as a string.- Parameters:
- source- the URL to read from
- Returns:
- a string containing all the characters read from the URL
- Throws:
- FileUtilsErrorException- if an error occurs while reading the URL
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
readBytesReads all the bytes from theInputStream.- Parameters:
- inputStream- the inputStream to read from
- Returns:
- a byte array containing all the bytes read from the input stream
- Throws:
- FileUtilsErrorException- if an error occurs while reading the input stream
- IllegalArgumentException- if inputStream is null
- Since:
- 1.0
 
- 
readBytesReads all the bytes from the file denoted by the specifiedFileobject.- Parameters:
- source- the file to read from
- Returns:
- a byte array containing all the bytes read from the file
- Throws:
- FileUtilsErrorException- if an error occurs while reading the file
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
readBytesReads all the bytes from theURL.- Parameters:
- source- the URL to read from
- Returns:
- a byte array containing all the bytes read from the URL
- Throws:
- FileUtilsErrorException- if an error occurs while reading the URL
- IllegalArgumentException- if source is null
- Since:
- 1.0
 
- 
writeBytesWrites a byte array to a given file.- Parameters:
- content- the byte array to be written
- destination- the file to which the byte array should be written
- Throws:
- FileUtilsErrorException- if there is an error writing the byte array to the file
- IllegalArgumentException- if either the content or destination arguments are null
- Since:
- 1.0
 
- 
writeStringWrites a string to a given file.- Parameters:
- content- the string to be written
- destination- the file to which the string should be written
- Throws:
- FileUtilsErrorException- if there is an error writing the string to the file
- IllegalArgumentException- if either the content or destination arguments are null
- Since:
- 1.0
 
- 
deleteFileDeletes a given file.- Parameters:
- file- The file to be deleted.
- Throws:
- IllegalArgumentException- if file is null.
- Since:
- 1.0
 
- 
getUniqueFilenameGenerates a unique filename.- Returns:
- A unique filename string.
- Since:
- 1.0
 
- 
unzipFileUnzips a file from the source location to a destination.- Parameters:
- source- the file to unzip (must not be null)
- destination- the location to unzip the file to (must not be null)
- Throws:
- FileUtilsErrorException- if an error occurs while unzipping the file or creating directories
- Since:
- 1.0
 
- 
getBaseNameReturns the base name of the specified file, without the extension.- Parameters:
- file- The file whose base name is to be returned.
- Returns:
- The base name of the file.
- Since:
- 1.0
 
- 
getBaseNameReturns the base name of the specified file name, without the extension.- Parameters:
- fileName- The name of the file whose base name is to be returned.
- Returns:
- The base name of the file.
- Throws:
- IllegalArgumentException- if fileName is null.
- Since:
- 1.0
 
- 
getExtensionReturns the extension of the specifiedFile.- Parameters:
- file- the file to get the extension from
- Returns:
- the extension of the given file or null if the file has no extension.
- Throws:
- IllegalArgumentException- if file is null
- Since:
- 1.0
 
- 
getExtensionReturns the extension of the specified file name.- Parameters:
- fileName- the name of the file to get the extension from
- Returns:
- the extension of the given file name or null if the file name has no extension.
- Throws:
- IllegalArgumentException- if fileName is null
- Since:
- 1.0
 
- 
combineToAbsolutePathsCombines an arbitrary number of lists of files into a single list of absolute file paths.- Parameters:
- files- zero or more lists of files to be combined
- Returns:
- the list of absolute file paths resulting from combining the input lists
- Since:
- 1.5.2
 
- 
generateDirectoryListingGenerates a sorted directory listing of all files and subdirectories in the specified directory.This will be formatted for Posix use. - Parameters:
- directory- The root directory to list.
- Returns:
- A string containing a sorted directory listing of all files and subdirectories in the specified directory.
- Throws:
- IOException- If an error occurs while listing the directory.
- Since:
- 1.5.0
 
- 
joinPathsJoins a list of file paths into a single string with the file path separator.- Parameters:
- paths- A list of file paths to join.
- Returns:
- A single string consisting of all the input file paths separated by the file path separator, or an empty string if input list is null.
- Since:
- 1.5.2
 
- 
getJavaFileListReturns a list of Java files in the specified directory.- Parameters:
- directory- The directory to search for Java files.
- Returns:
- A list of File objects pointing to the Java files in the directory, or an empty list if input directory is null.
- Since:
- 1.5.10
 
- 
transformFilespublic static void transformFiles(File directory, Function<String, String> transformer) throws FileUtilsErrorExceptionTransforms all files in the specified directory using the provided transformation function.- Parameters:
- directory- The directory containing the files to be transformed.
- transformer- The transformation function to be applied to each file.
- Throws:
- FileUtilsErrorException- If there is an error while reading or writing a file.
- Since:
- 1.5.10
 
- 
transformFilespublic static void transformFiles(File directory, Pattern included, Pattern excluded, Function<String, String> transformer) throws FileUtilsErrorExceptionTransforms files in the specified directory that match the included pattern and do not match the excluded pattern using the provided transformation function.- Parameters:
- directory- The directory containing the files to be transformed.
- included- A regex pattern specifying which files to include.
- excluded- A regex pattern specifying which files to exclude.
- transformer- The transformation function to be applied to each file.
- Throws:
- FileUtilsErrorException- If there is an error while reading or writing a file.
- Since:
- 1.5.10
 
- 
transformFilespublic static void transformFiles(File directory, List<Pattern> included, List<Pattern> excluded, Function<String, String> transformer) throws FileUtilsErrorExceptionTransforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.- Parameters:
- directory- The directory containing the files to be transformed.
- included- A list of regex patterns specifying which files to include.
- excluded- A list of regex patterns specifying which files to exclude.
- transformer- The transformation function to be applied to each file.
- Throws:
- FileUtilsErrorException- If there is an error while reading or writing a file.
- Since:
- 1.5.10
 
- 
transformFilespublic static void transformFiles(File directory, Pattern[] included, Pattern[] excluded, Function<String, String> transformer) throws FileUtilsErrorExceptionTransforms files in the specified directory that match at least one of the included patterns and do not match any of the excluded patterns using the provided transformation function.- Parameters:
- directory- The directory containing the files to be transformed.
- included- An array of regex patterns specifying which files to include.
- excluded- An array of regex patterns specifying which files to exclude.
- transformer- The transformation function to be applied to each file.
- Throws:
- FileUtilsErrorException- If there is an error while reading or writing a file.
- Since:
- 1.5.10
 
- 
permissionsFromModeCreates a new set ofPosixFilePermissionbased on the given Posix mode.Standard Posix permissions can be provided on octal numbers, for instance 0755and0644.- Parameters:
- mode- an integer containing permissions for the owner, group and others
- Returns:
- a Set of PosixFilePermission
- Since:
- 1.5.19
 
- 
pathConstructs a newPathinstance using the specified file and additional path elements.- Parameters:
- file- The file to use as the starting point for the Path.
- paths- Additional path elements to add to the Path.
- Returns:
- A new Path instance.
- Since:
- 1.5.19
 
- 
pathConstructs a newPathinstance using the specified file and additional path elements.- Parameters:
- path- The path to use as the starting point for the Path.
- paths- Additional path elements to add to the Path.
- Returns:
- A new Path instance.
- Since:
- 1.5.19
 
- 
versionFromResourceReads the contents of a resource for use as a version number.- Parameters:
- name- the name of the resource to look for
- Returns:
- the version number; or "unknown version" if the version resource couldn't be found
- Since:
- 1.7
 
 
-