Package nl.colorize.util
Class FileUtils
java.lang.Object
nl.colorize.util.FileUtils
Utility class for loading data from and saving data to files. Unless stated
otherwise, all methods in this class will throw an
IOException
if
an I/O error occurs while interacting with the file.
When using Java 7 or newer, some of the convenience methods in this class
are no longer needed and can be replaced with Files
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyDirectory
(File source, File target) Copies a directory to the specified location.static long
countDirectorySize
(File dir) Returns the size of the specified directory, in bytes.static File
Creates a temporary directory within the platform's default location for storing temporary files.static File
createTempDir
(String name) Creates a temporary directory with the specified name.static File
createTempFile
(byte[] data) Creates a temporary file with the specified contents.static File
createTempFile
(String text, Charset encoding) Creates a temporary file with the specified text as contents.static void
Deletes a file.static void
deleteDirectory
(File dir) Deletes a directory and all of its contents.static File
expandUser
(String path) Expands a file path that contains a reference to the user's home directory.static String
formatFileSize
(long fileSize) Returns a human-readable description of the specified file size in bytes.static String
formatFileSize
(File file) Returns a human-readable description of the specified file's size.static String
getFileExtension
(File file) Returns the file extension of the specified file, excluding the dot.static String
getRelativePath
(File file, File base) Deprecated.static void
Creates a directory.static File
Creates a new directory with the specified name and location, then returns the directory that was just created.static void
Reads all lines in a file, then used the provided callback function to rewrite the lines.static void
rewriteLine
(File file, String original, String replacement, Charset charset) Reads all lines in a file, then replaced all matching lines with the specified replacement.Shorthand forFiles.walk
to iterate over files in a directory, with only files matching the filter being returned.
-
Method Details
-
rewrite
public static void rewrite(File file, Charset charset, Function<String, String> mapper) throws IOExceptionReads all lines in a file, then used the provided callback function to rewrite the lines. The result is then used to overwrite the original file.- Throws:
IOException
-
rewriteLine
public static void rewriteLine(File file, String original, String replacement, Charset charset) throws IOException Reads all lines in a file, then replaced all matching lines with the specified replacement. The result is then used to overwrite the original file.- Throws:
IOException
-
expandUser
Expands a file path that contains a reference to the user's home directory. For example, if the path is "~/Desktop/test.txt" and the user home directory is "/home/john", the returned file will reference "/home/john/Desktop/test.txt".- Throws:
IllegalArgumentException
- if the path is empty.UnsupportedOperationException
- if the current platform does not provide or support a user home directory.
-
mkdir
Creates a directory. UnlikeFile.mkdir()
, an exception will be thrown if the directory could not be created. If the directory already exists this method does nothing.- Throws:
IOException
-
mkdir
Creates a new directory with the specified name and location, then returns the directory that was just created.- Throws:
IOException
-
delete
Deletes a file. UnlikeFile.delete()
, an exception will be thrown if the file could not be deleted. If the file does not exist this method does nothing.- Throws:
IOException
-
copyDirectory
Copies a directory to the specified location.- Throws:
IllegalStateException
- if the target directory already exists.IOException
- if an I/O error occurs while copying the files.
-
deleteDirectory
Deletes a directory and all of its contents. If the directory does not exist this method does nothing.- Throws:
IOException
- if an I/O error occurs while deleting.IllegalArgumentException
- if the argument is not a directory.
-
walkFiles
Shorthand forFiles.walk
to iterate over files in a directory, with only files matching the filter being returned.- Throws:
IOException
-
getRelativePath
Deprecated.UsePath.relativize
instead. This can be used even when working with APIs still usingFile
instances:base.toPath().relativize(file.toPath)
.Returnsfile
's path relative tobase
. For example, the path "/a/b/c/d.txt" relative to "/a/b" would return "c/d.txt". If both paths do not share a common basefile
's absolute path will be returned. -
getFileExtension
Returns the file extension of the specified file, excluding the dot. So the file "test.png" would return "png". Note that the file extension will be returned as lowercase, even if the original file name was not. If the file does not have a file extension this will return an aempty string. -
countDirectorySize
Returns the size of the specified directory, in bytes.- Throws:
IOException
-
formatFileSize
Returns a human-readable description of the specified file's size. The level of precision is automatically selected based on the value. -
formatFileSize
Returns a human-readable description of the specified file size in bytes. The level of precision is automatically selected based on the value. -
createTempFile
Creates a temporary file with the specified contents. The file will be created in the platform's default location for storing temporary files.- Throws:
IOException
-
createTempFile
Creates a temporary file with the specified text as contents. The file will be created in the platform's default location for storing temporary files.- Throws:
IOException
-
createTempDir
Creates a temporary directory within the platform's default location for storing temporary files.- Throws:
IOException
-
createTempDir
Creates a temporary directory with the specified name. The directory will be created in the platform's default location for storing temporary files.- Throws:
IOException
-
Path.relativize
instead.