Package rife.tools
Class FileUtils
java.lang.Object
rife.tools.FileUtils
A utility class for handling files.
- Since:
- 1.0
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptioncombineToAbsolutePaths
(List<File>... files) Combines an arbitrary number of lists of files into a single list of absolute file paths.static void
Copy the contents of one file to another file.static void
copy
(File source, OutputStream outputStream) Copy the contents of a file to an OutputStream.static void
copy
(InputStream inputStream, File target) Copy the contents of an InputStream to a file.static void
copy
(InputStream inputStream, OutputStream outputStream) Copy the contents of an InputStream to an OutputStream.static void
copyDirectory
(File sourceDir, File targetDir) Copies all files and directories from the source directory to the target directory.static void
deleteDirectory
(File source) Deletes a directory and all its contents recursively.static void
deleteFile
(File file) Deletes a given file.static String
generateDirectoryListing
(File directory) Generates a sorted directory listing of all files and subdirectories in the specified directory.static String
getBaseName
(File file) Returns the base name of the specified file, without the extension.static String
getBaseName
(String fileName) Returns the base name of the specified file name, without the extension.static String
getExtension
(File file) Returns the extension of the specifiedFile
.static String
getExtension
(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 String
Generates a unique filename.static String
Joins a list of file paths into a single string with the file path separator.static void
moveDirectory
(File source, File target) Moves the source directory to the target location.static void
Moves the source file to the target location.static Path
Constructs a newPath
instance using the specified file and additional path elements.static Path
Constructs a newPath
instance using the specified file and additional path elements.static Set<PosixFilePermission>
permissionsFromMode
(int mode) Creates a new set ofPosixFilePermission
based 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 specifiedFile
object.static ByteArrayOutputStream
readStream
(InputStream inputStream) Reads the complete contents of an input stream and returns it as a ByteArrayOutputStream object.static String
readString
(File source) Reads the content of the given file into a string using the default encoding.static String
readString
(File source, String encoding) Reads the content of the given file into a string using the specified character encoding.static String
readString
(InputStream inputStream) Reads the entire contents of an InputStream and returns it as a String, using UTF-8 encoding.static String
readString
(InputStream inputStream, String encoding) Reads the entire contents of an InputStream and returns it as a String, using the specified character encoding.static String
readString
(Reader reader) Reads the entire contents of a Reader and returns it as a String.static String
readString
(URL source) Reads all characters from theURL
and returns its contents as a string.static String
readString
(URL source, String encoding) Reads the content of the given URL into a string.static void
transformFiles
(File directory, Function<String, String> transformer) Transforms all files in the specified directory using the provided transformation function.static void
transformFiles
(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 void
transformFiles
(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 void
transformFiles
(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 void
Unzips a file from the source location to a destination.static String
versionFromResource
(String name) Reads the contents of a resource for use as a version number.static void
writeBytes
(byte[] content, File destination) Writes a byte array to a given file.static void
writeString
(String content, File destination) Writes a string to a given file.
-
Field Details
-
JAVA_FILE_PATTERN
-
JAR_FILE_PATTERN
-
-
Method Details
-
getFileList
Returns 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
-
getFileList
Returns 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
-
getFileList
Returns 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
-
getFileList
Returns 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
-
moveFile
Moves 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
-
moveDirectory
Moves 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
-
deleteDirectory
Deletes 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
-
copy
public 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
-
copy
Copy the contents of an InputStream to a file.- Parameters:
inputStream
- the InputStream to copy from; must not be nulltarget
- the File to copy to; must not be null- Throws:
FileUtilsErrorException
- if an error occurs while copying the stream to the fileIllegalArgumentException
- if inputStream or target is null- Since:
- 1.0
-
copy
Copy the contents of a file to an OutputStream.- Parameters:
source
- the File to copy from; must not be nulloutputStream
- the OutputStream to copy to; must not be null- Throws:
FileUtilsErrorException
- if an error occurs while copying the file to the streamIllegalArgumentException
- if source or outputStream is null- Since:
- 1.0
-
copy
Copy the contents of one file to another file.- Parameters:
source
- the File to copy from; must not be nulltarget
- the File to copy to; must not be null- Throws:
FileUtilsErrorException
- if an error occurs while copying the source file to the target fileIllegalArgumentException
- if source or target is null- Since:
- 1.0
-
copyDirectory
Copies all files and directories from the source directory to the target directory.- Parameters:
sourceDir
- the directory to be copied fromtargetDir
- the directory to be copied to- Throws:
FileUtilsErrorException
- if there's an error copying the filesIllegalArgumentException
- if either sourceDir or targetDir are null- Since:
- 1.0
-
readStream
public 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
-
readString
Reads 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
-
readString
Reads 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
-
readString
public 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
-
readString
Reads the content of the given URL into a string.- Parameters:
source
- the URL to read fromencoding
- 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 URLIllegalArgumentException
- if source is null- Since:
- 1.0
-
readString
Reads 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 fileIllegalArgumentException
- if source is null- Since:
- 1.0
-
readString
Reads the content of the given file into a string using the specified character encoding.- Parameters:
source
- the file to read fromencoding
- 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 fileIllegalArgumentException
- if source is null- Since:
- 1.0
-
readString
Reads all characters from theURL
and 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 URLIllegalArgumentException
- if source is null- Since:
- 1.0
-
readBytes
Reads 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 streamIllegalArgumentException
- if inputStream is null- Since:
- 1.0
-
readBytes
Reads all the bytes from the file denoted by the specifiedFile
object.- 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 fileIllegalArgumentException
- if source is null- Since:
- 1.0
-
readBytes
Reads 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 URLIllegalArgumentException
- if source is null- Since:
- 1.0
-
writeBytes
Writes a byte array to a given file.- Parameters:
content
- the byte array to be writtendestination
- the file to which the byte array should be written- Throws:
FileUtilsErrorException
- if there is an error writing the byte array to the fileIllegalArgumentException
- if either the content or destination arguments are null- Since:
- 1.0
-
writeString
Writes a string to a given file.- Parameters:
content
- the string to be writtendestination
- the file to which the string should be written- Throws:
FileUtilsErrorException
- if there is an error writing the string to the fileIllegalArgumentException
- if either the content or destination arguments are null- Since:
- 1.0
-
deleteFile
Deletes a given file.- Parameters:
file
- The file to be deleted.- Throws:
IllegalArgumentException
- if file is null.- Since:
- 1.0
-
getUniqueFilename
Generates a unique filename.- Returns:
- A unique filename string.
- Since:
- 1.0
-
unzipFile
Unzips 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
-
getBaseName
Returns 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
-
getBaseName
Returns 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
-
getExtension
Returns 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
-
getExtension
Returns 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
-
combineToAbsolutePaths
Combines 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
-
generateDirectoryListing
Generates 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
-
joinPaths
Joins 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
-
getJavaFileList
Returns 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
-
transformFiles
public 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
-
transformFiles
public 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
-
transformFiles
public 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
-
transformFiles
public 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
-
permissionsFromMode
Creates a new set ofPosixFilePermission
based on the given Posix mode.Standard Posix permissions can be provided on octal numbers, for instance
0755
and0644
.- Parameters:
mode
- an integer containing permissions for the owner, group and others- Returns:
- a Set of PosixFilePermission
- Since:
- 1.5.19
-
path
Constructs a newPath
instance 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
-
path
Constructs a newPath
instance 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
-
versionFromResource
Reads 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
-