public final class FileUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
private static long |
FILE_COPY_BUFFER_SIZE
The file copy buffer size (30 MB)
|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
private static char |
SYSTEM_SEPARATOR
The system separator character.
|
private static char |
WINDOWS_SEPARATOR
The Windows separator character.
|
| Modifier | Constructor and Description |
|---|---|
private |
FileUtils()
Creates a new instance of FileUtils.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
cleanDirectory(File directory)
Deletes a directory recursively.
|
static void |
deleteDirectory(File directory)
Deletes a directory recursively.
|
static boolean |
deleteQuietly(File file)
Deletes a file, never throwing an exception.
|
static void |
forceDelete(File file)
Deletes a file.
|
static File |
getTempDirectory()
Returns a
File representing the system temporary directory. |
static String |
getTempDirectoryPath()
Returns the path to the system temporary directory.
|
static boolean |
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.
|
static InputStream |
openInputStream(File file)
Opens a
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file). |
static OutputStream |
openOutputStream(File file)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static OutputStream |
openOutputStream(File file,
boolean append)
Opens a
FileOutputStream for 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 String |
readFileToString(File file)
Deprecated.
2.5 use
readFileToString(File, Charset) instead |
static String |
readFileToString(File file,
Charset encoding)
Reads the contents of a file into a String.
|
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static List<String> |
readLines(File file)
Deprecated.
2.5 use
readLines(File, Charset) instead |
static List<String> |
readLines(File file,
Charset encoding)
Reads the contents of a file line by line to a List of Strings.
|
static void |
writeByteArrayToFile(File file,
byte[] data)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(File file,
byte[] data,
boolean append)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(File file,
byte[] data,
int off,
int len,
boolean append)
Writes
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist. |
static void |
writeStringToFile(File file,
String data)
Deprecated.
2.5 use
writeStringToFile(File, String, Charset, boolean) instead |
static void |
writeStringToFile(File file,
String data,
Charset encoding,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(File file,
String data,
String encoding)
Writes a String to a file creating the file if it does not exist.
|
private static final char WINDOWS_SEPARATOR
private static final char SYSTEM_SEPARATOR
public static final long ONE_KB
public static final long ONE_MB
private static final long FILE_COPY_BUFFER_SIZE
public static void deleteDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion is unsuccessfulpublic static boolean isSymlink(File file) throws IOException
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 false if the system
is detected as Windows
For code that runs on Java 1.7 or later, use the following method instead:
boolean java.nio.file.Files.isSymbolicLink(Path path)
file - the file to checkIOException - if an IO error occurs while checking the filepublic static void cleanDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion is unsuccessfulpublic static void forceDelete(File file) throws IOException
The difference between File.delete() and this method are:
file - file or directory to delete, must not be nullNullPointerException - if the directory is nullFileNotFoundException - if the file was not foundIOException - in case deletion is unsuccessfulpublic static String getTempDirectoryPath()
@Deprecated public static String readFileToString(File file) throws IOException
readFileToString(File, Charset) insteadfile - the file to read, must not be nullnullIOException - in case of an I/O errorpublic static String readFileToString(File file, Charset encoding) throws IOException
file - the file to read, must not be nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O errorpublic static String readFileToString(File file, String encoding) throws IOException
file - the file to read, must not be nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O errorpublic static InputStream openInputStream(File file) throws IOException
FileInputStream for the specified file, providing better
error messages than simply calling new 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.
file - the file to open for input, must not be nullInputStream for the specified fileFileNotFoundException - if the file does not existIOException - if the file object is a directoryIOException - if the file cannot be read@Deprecated public static void writeStringToFile(File file, String data) throws IOException
writeStringToFile(File, String, Charset, boolean) insteadfile - the file to writedata - the content to write to the fileIOException - in case of an I/O errorpublic static void writeStringToFile(File file, String data, String encoding) throws IOException
file - the file to writedata - the content to write to the fileencoding - the encoding to use, null means platform defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException
file - the file to writedata - the content to write to the fileencoding - the encoding to use, null means platform defaultappend - if true, then the String will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static OutputStream openOutputStream(File file, boolean append) throws IOException
FileOutputStream for 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.
file - the file to open for output, must not be nullappend - if true, then bytes will be added to the
end of the file rather than overwritingOutputStream for the specified fileIOException - if the file object is a directoryIOException - if the file cannot be written toIOException - if a parent directory needs creating but that failspublic static File getTempDirectory()
File representing the system temporary directory.public static boolean deleteQuietly(File file)
The difference between File.delete() and this method are:
file - file or directory to delete, can be nulltrue if the file or directory was deleted, otherwise
falsepublic static void writeByteArrayToFile(File file, byte[] data) throws IOException
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write todata - the content to write to the fileIOException - in case of an I/O erroepublic static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
file - the file to write todata - the content to write to the fileappend - if true, then bytes will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist.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 - if true, then bytes will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static byte[] readFileToByteArray(File file) throws IOException
file - the file to read, must not be nullnullIOException - in case of an I/O errorpublic static OutputStream openOutputStream(File file) throws IOException
FileOutputStream for 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.
file - the file to open for output, must not be nullOutputStream for the specified fileIOException - if the file object is a directoryIOException - if the file cannot be written toIOException - if a parent directory needs creating but that fails@Deprecated public static List<String> readLines(File file) throws IOException
readLines(File, Charset) insteadfile - the file to read, must not be nullnullIOException - in case of an I/O errorpublic static List<String> readLines(File file, Charset encoding) throws IOException
file - the file to read, must not be nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O errorCopyright © 2003–2019 The Apache Software Foundation. All rights reserved.