Class FileUtils
- Author:
- Apache Directory Project
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanDirectory(File directory) Deletes a directory recursively.static voiddeleteDirectory(File directory) Deletes a directory recursively.static booleandeleteQuietly(File file) Deletes a file, never throwing an exception.static voidforceDelete(File file) Deletes a file.static FileReturns aFilerepresenting the system temporary directory.static StringReturns the path to the system temporary directory.static booleanDetermines whether the specified file is a Symbolic Link rather than an actual file.static InputStreamopenInputStream(File file) Opens aFileInputStreamfor the specified file, providing better error messages than simply callingnew FileInputStream(file).static OutputStreamopenOutputStream(File file) Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.static OutputStreamopenOutputStream(File file, boolean append) Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.static byte[]readFileToByteArray(File file) Reads the contents of a file into a byte array.static StringreadFileToString(File file) Deprecated.static StringreadFileToString(File file, String encoding) Reads the contents of a file into a String.static StringreadFileToString(File file, Charset encoding) Reads the contents of a file into a String.Deprecated.2.5 usereadLines(File, Charset)insteadReads the contents of a file line by line to a List of Strings.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 voidwriteByteArrayToFile(File file, byte[] data, int off, int len, boolean append) Writeslenbytes from the specified byte array starting at offsetoffto a file, creating the file if it does not exist.static voidwriteStringToFile(File file, String data) Deprecated.2.5 usewriteStringToFile(File, String, Charset, boolean)insteadstatic voidwriteStringToFile(File file, String data, String encoding) Writes a String to a file creating the file if it does not exist.static voidwriteStringToFile(File file, String data, Charset encoding, boolean append) Writes a String to a file creating the file if it does not exist.
-
Field Details
-
ONE_KB
public static final long ONE_KBThe number of bytes in a kilobyte.- See Also:
-
ONE_MB
public static final long ONE_MBThe number of bytes in a megabyte.- See Also:
-
-
Method Details
-
deleteDirectory
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
isSymlink
Determines whether the specified file is a Symbolic Link rather than an actual file.Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.
Note: the current implementation always returns
falseif the system is detected as WindowsFor code that runs on Java 1.7 or later, use the following method instead:
boolean java.nio.file.Files.isSymbolicLink(Path path)- Parameters:
file- the file to check- Returns:
- true if the file is a Symbolic Link
- Throws:
IOException- if an IO error occurs while checking the file- Since:
- 2.0
-
cleanDirectory
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
forceDelete
Deletes a file. If file is a directory, delete it and all sub-directories.The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
- Parameters:
file- file or directory to delete, must not benull- Throws:
IOException- in case deletion is unsuccessful
-
getTempDirectoryPath
Returns the path to the system temporary directory.- Returns:
- the path to the system temporary directory.
- Since:
- 2.0
-
readFileToString
Deprecated.2.5 usereadFileToString(File, Charset)insteadReads the contents of a file into a String using the default encoding for the VM. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.3.1
-
readFileToString
Reads the contents of a file into a String. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
readFileToString
Reads the contents of a file into a String. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
openInputStream
Opens aFileInputStreamfor the specified file, providing better error messages than simply callingnew FileInputStream(file).At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
- Parameters:
file- the file to open for input, must not benull- Returns:
- a new
InputStreamfor the specified file - Throws:
FileNotFoundException- if the file does not existIOException- if the file object is a directoryIOException- if the file cannot be read- Since:
- 1.3
-
writeStringToFile
Deprecated.2.5 usewriteStringToFile(File, String, Charset, boolean)insteadWrites a String to a file creating the file if it does not exist using the default encoding for the VM.- Parameters:
file- the file to writedata- the content to write to the file- Throws:
IOException- in case of an I/O error
-
writeStringToFile
Writes a String to a file creating the file if it does not exist. NOTE: As from v1.3, 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 fileencoding- the encoding to use,nullmeans platform default- Throws:
IOException- in case of an I/O errorUnsupportedEncodingException- if the encoding is not supported by the VM
-
writeStringToFile
public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException Writes a String to a file creating the file if it does not exist.- Parameters:
file- the file to writedata- the content to write to the fileencoding- the encoding to use,nullmeans platform defaultappend- iftrue, then the String will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
openOutputStream
Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
- Parameters:
file- the file to open for output, must not benullappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Returns:
- a new
OutputStreamfor the specified file - Throws:
IOException- if the file object is a directoryIOException- if the file cannot be written toIOException- if a parent directory needs creating but that fails- Since:
- 2.1
-
getTempDirectory
-
deleteQuietly
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- No exceptions are thrown when a file or directory cannot be deleted.
- Parameters:
file- file or directory to delete, can benull- Returns:
trueif the file or directory was deleted, otherwisefalse- Since:
- 1.4
-
writeByteArrayToFile
Writes a byte array to a file creating the file if it does not exist.NOTE: As from v1.3, 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 erroe- Since:
- 1.1
-
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- Since:
- 2.1
-
writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException Writeslenbytes from the specified byte array starting at offsetoffto a file, creating the file if it does not exist.- Parameters:
file- the file to write todata- the content to write to the fileoff- the start offset in the datalen- the number of bytes to writeappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error- Since:
- 2.5
-
readFileToByteArray
Reads the contents of a file into a byte array. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.1
-
openOutputStream
Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
- Parameters:
file- the file to open for output, must not benull- Returns:
- a new
OutputStreamfor the specified file - Throws:
IOException- if the file object is a directoryIOException- if the file cannot be written toIOException- if a parent directory needs creating but that fails- Since:
- 1.3
-
readLines
Deprecated.2.5 usereadLines(File, Charset)insteadReads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the list of Strings representing each line in the file, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.3
-
readLines
Reads the contents of a file line by line to a List of Strings. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the list of Strings representing each line in the file, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
readFileToString(File, Charset)instead