Class FileUtils
java.lang.Object
de.iip_ecosphere.platform.support.FileUtils
Basic file functionality.
- Author:
- Holger Eichelberger, SSE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbase64ToFile(String string, File file) Turn base64 encodedstringinto afile.static voidcleanDirectory(File directory) Recursively deletes all files and subdirectories within a given directory.static voidcloseQuietly(Closeable closable) Closes a closable quietly.static booleancontentEquals(File file1, File file2) Tests whether the contents of two files are equal.static voidcopyDirectory(File srcDir, File destDir) Copies a whole directory to a new location, preserving the file dates.static voidcopyDirectory(File srcDir, File destDir, FileFilter filter) Copies a filtered directory to a new location preserving the file dates.static voidcopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) Copies a filtered directory to a new location.static voidCopies a file to a new location preserving the file date.static voidcopyInputStreamToFile(InputStream source, File destination) static FilecreateTmpFolder(String name) Creates a temporary folder injava.io.tmpdirwithout cleanup.static FilecreateTmpFolder(String name, boolean cleanup) Creates a temporary folder injava.io.tmpdir.static voiddeleteDirectory(File directory) Deletes a directory recursively.static voiddeleteOnExit(File file) Deletes a file or directory on JVM exit, not throwing an exception.static booleandeleteQuietly(File file) Deletes a file or directory, not throwing an exception.static StringfileToBase64(File file) Turnfileinto a base64 encoded string.static FileTries to find the filenameinfolderand its subfolders.static voidforceDelete(File file) Deletes a file or directory.static FilegetResolvedFile(File path) Resolves a path and returns the canonical/absolute path.static StringgetResolvedPath(File path, String name) Composes a path and returns the canonical/absolute path (single-time use).static FileReturns the system root folder.static FileReturns aFilerepresenting the system temporary directory.static StringReturns the path to the system temporary directory.static FileReturns aFilerepresenting the user's home directory.static StringReturns the path to the user's home directory.static voidLists contained files.static byte[]readFileToByteArray(File file) Reads the contents of a file into a byte array.static StringreadFileToString(File file) Reads the contents of a file into a String with platform default charset.static StringreadFileToString(File file, Charset charset) Reads the contents of a file into a String.static StringsanitizeFileName(String str) Turns an arbitrary string into something that can be used as a file name.static StringsanitizeFileName(String str, boolean addTimestamp) Turns an arbitrary string into something that can be used as a file name.static voidwrite(File file, CharSequence data) Writes a CharSequence with platform default charset to a file creating the file if it does not exist.static voidwrite(File file, CharSequence data, Charset charset) Writes a CharSequence to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data) Writes a byte array to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data, boolean append) Writes a byte array to a file creating the file if it does not exist.static voidwriteStringToFile(File file, String data) Writes a String with platform default charset to a file creating the file if it does not exist.static voidwriteStringToFile(File file, String data, Charset charset) Writes a String to a file creating the file if it does not exist.
-
Constructor Details
-
FileUtils
private FileUtils()Preventing external creation.
-
-
Method Details
-
createTmpFolder
Creates a temporary folder injava.io.tmpdirwithout cleanup.- Parameters:
name- the name of the temporary folder within the system/user temporary directory- Returns:
- the temporary folder (descriptor)
-
createTmpFolder
Creates a temporary folder injava.io.tmpdir.- Parameters:
name- the name of the temporary folder within the system/user temporary directorycleanup- try to do an auto cleanup at JVM shutdown- Returns:
- the temporary folder (descriptor), may receive a suffix if there is already a folder that cannot be deleted
-
deleteQuietly
Deletes a file or directory, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]- Parameters:
file- file or directory to delete, may benull- Returns:
trueiffilewas deleted, otherwisefalse
-
cleanDirectory
Recursively deletes all files and subdirectories within a given directory.- Parameters:
directory- The directory to clean.- Throws:
IOException- if deleting/cleaning fails
-
forceDelete
Deletes a file or directory. For a directory, delete it and all subdirectories.- Parameters:
file- file or directory to delete- Throws:
FileNotFoundException- if the file was not foundIOException- in case deletion is unsuccessful
-
deleteOnExit
Deletes a file or directory on JVM exit, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]- Parameters:
file- file or directory to delete, may benull
-
getTempDirectoryPath
Returns the path to the system temporary directory. [convenience]- Returns:
- the path to the system temporary directory.
-
getTempDirectory
Returns aFilerepresenting the system temporary directory. [convenience]- Returns:
- the system temporary directory.
-
getUserDirectory
Returns aFilerepresenting the user's home directory.- Returns:
- the user's home directory.
-
getUserDirectoryPath
Returns the path to the user's home directory.- Returns:
- the path to the user's home directory.
-
closeQuietly
Closes a closable quietly.- Parameters:
closable- the closable, may be null
-
listFiles
Lists contained files.- Parameters:
file- the file/folder to listaccept- accept the file for further (nested) listing, not called for foldershandle- handle an accepted file
-
getResolvedPath
Composes a path and returns the canonical/absolute path (single-time use).- Parameters:
path- the pathname- the name to be added (may be a path, may be empty or null)- Returns:
- the canonical/absolute path
-
getResolvedFile
Resolves a path and returns the canonical/absolute path.- Parameters:
path- the path- Returns:
- the canonical/absolute path
-
fileToBase64
Turnfileinto a base64 encoded string.- Parameters:
file- the file- Returns:
- the base64 encoded string
- Throws:
IOException- iffilecannot be read
-
base64ToFile
Turn base64 encodedstringinto afile.- Parameters:
string- the base64 encoded stringfile- the file- Throws:
IOException- iffilecannot be written
-
sanitizeFileName
Turns an arbitrary string into something that can be used as a file name.- Parameters:
str- the string to use- Returns:
- the file name
-
sanitizeFileName
Turns an arbitrary string into something that can be used as a file name.- Parameters:
str- the string to useaddTimestamp- whether the current timestamp shall be added tostr- Returns:
- the file name
- See Also:
-
findFile
Tries to find the filenameinfolderand its subfolders.- Parameters:
folder- the folder to searchname- the file name to find- Returns:
- the found file or null for none
-
getSystemRoot
Returns the system root folder.- Returns:
- the system root
-
write
Writes a CharSequence with platform default charset to a file creating the file if it does not exist.- Parameters:
file- the file to writedata- the content to write to the file- Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
write
Writes a CharSequence to a file creating the file if it does not exist.- Parameters:
file- the file to writedata- the content to write to the filecharset- the requested charset, null means platform default- Throws:
IOException- in case of an I/O error
-
readFileToString
Reads the contents of a file into a String with platform default charset. Thefileis always closed.- Parameters:
file- the file to read- Returns:
- the file contents
- Throws:
IOException- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
-
readFileToString
Reads the contents of a file into a String. Thefileis always closed.- Parameters:
file- the file to readcharset- the requested charset, null means platform default- Returns:
- the file contents
- Throws:
IOException- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
-
deleteDirectory
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessfulIllegalArgumentException- ifdirectoryis not a directory
-
copyDirectory
Copies a filtered directory to a new location preserving the file dates. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.- Parameters:
srcDir- an existing directory to copydestDir- the new directoryfilter- the filter to apply, null means copy all directories and files should be the same as the original- Throws:
IllegalArgumentException- ifsrcDirexists but is not a directory, or the source and the destination directory are the sameFileNotFoundException- if the source does not existIOException- if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
-
copyDirectory
public static void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException Copies a filtered directory to a new location. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.- Parameters:
srcDir- an existing directory to copydestDir- the new directoryfilter- the filter to apply, null means copy all directories and filespreserveFileDate-trueif the file date of the copy should be the same as the original- Throws:
IllegalArgumentException- ifsrcDirexists but is not a directory, the source and the destination directory are the same, or the destination is not writableFileNotFoundException- if the source does not existIOException- if an error occurs or setting the last-modified time didn't succeed
-
copyDirectory
Copies a whole directory to a new location, preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory. That is, copying /home/bar to /tmp/bang copies the contents of /home/bar into /tmp/bang. It does not create /tmp/bang/bar. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.- Parameters:
srcDir- an existing directory to copydestDir- the new directory- Throws:
IllegalArgumentException- ifsrcDirexists but is not a directory, the source and the destination directory are the sameFileNotFoundException- if the source does not exist.IOException- if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
-
copyFile
Copies a file to a new location preserving the file date. This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method overwrites it. A symbolic link is resolved before copying so the new file is not a link.- Parameters:
srcFile- an existing file to copydestFile- the new file- Throws:
IOException- if source or destination is invalid, if an error occurs or setting the last-modified time didn't succeed, if the output file length is not the same as the input file length after the copy completes
-
writeStringToFile
Writes a String with platform default charset to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.- Parameters:
file- the file to writedata- the content to write to the file- Throws:
IOException- in case of an I/O errorUnsupportedEncodingException- if the encoding is not supported by the VM
-
writeStringToFile
Writes a String to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.- Parameters:
file- the file to writedata- the content to write to the filecharset- the charset to use, null means platform default- Throws:
IOException- in case of an I/O errorUnsupportedEncodingException- if the encoding is not supported by the VM
-
readFileToByteArray
Reads the contents of a file into a byte array. Thefileis always closed.- Parameters:
file- the file to read- Returns:
- the file contents
- Throws:
IOException- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
-
writeByteArrayToFile
Writes a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.- Parameters:
file- the file to write todata- the content to write to the file- Throws:
IOException- in case of an I/O error
-
writeByteArrayToFile
Writes a byte array to a file creating the file if it does not exist.- Parameters:
file- the file to write todata- the content to write to the fileappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error
-
copyInputStreamToFile
Copies bytes from anInputStreamsourceto a filedestination. The directories up todestinationwill be created if they don't already exist.destinationwill be overwritten if it already exists.- Parameters:
source- theInputStreamto copy bytes from, must not be, will be closeddestination- the non-directoryFileto write bytes to (possibly overwriting)- Throws:
IOException- ifdestinationis a directory, ifdestinationcannot be written, ifdestinationneeds creating but can't be, if an IO error occurs during copying
-
contentEquals
Tests whether the contents of two files are equal. This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.- Parameters:
file1- the first filefile2- the second file- Returns:
trueif the content of the files are equal or they both don't exist,falseotherwise- Throws:
IllegalArgumentException- when an input is not a file.IOException- If an I/O error occurs.
-