public final class FileUtils extends Object
Modifier and Type | Field and Description |
---|---|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(File directory)
Deletes a directory recursively.
|
static void |
copyFile(File srcFile,
File destFile)
Copies a file to a new location preserving the file date.
|
static void |
copyFile(File srcFile,
File destFile,
boolean preserveFileDate)
Copies a file to a new location.
|
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.
|
public static final long ONE_KB
public static final long ONE_MB
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 null
NullPointerException
- if the directory is null
FileNotFoundException
- 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 null
null
IOException
- in case of an I/O errorpublic static String readFileToString(File file, Charset encoding) throws IOException
file
- the file to read, must not be null
encoding
- the encoding to use, null
means platform defaultnull
IOException
- in case of an I/O errorpublic static String readFileToString(File file, String encoding) throws IOException
file
- the file to read, must not be null
encoding
- the encoding to use, null
means platform defaultnull
IOException
- 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 null
FileInputStream
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 null
append
- if true
, then bytes will be added to the
end of the file rather than overwritingFileOutputStream
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 null
true
if the file or directory was deleted, otherwise
false
public static void copyFile(File srcFile, File destFile) throws IOException
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 will overwrite it.
Note: This method tries to preserve the file's last
modified date/times using File.setLastModified(long)
, however
it is not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
NullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingIOException
- if the output file length is not the same as the input file length after the copy completescopyFile(File, File, boolean)
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
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 will overwrite it.
Note: Setting preserveFileDate
to
true
tries to preserve the file's last modified
date/times using File.setLastModified(long)
, however it is
not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
preserveFileDate
- true if the file date of the copy
should be the same as the originalNullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingIOException
- if the output file length is not the same as the input file length after the copy completespublic 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 null
null
IOException
- 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 null
FileOutputStream
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 null
null
IOException
- 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 null
encoding
- the encoding to use, null
means platform defaultnull
IOException
- in case of an I/O errorCopyright © 2003–2018 The Apache Software Foundation. All rights reserved.