public class FileUtils extends Object
| 构造器和说明 |
|---|
FileUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
cleanDirectory(File directory)
Cleans a directory without deleting it.
|
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 void |
forceDeleteOnExit(File file)
Schedules a file to be deleted when JVM exits.
|
static boolean |
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.
|
static LineIterator |
lineIterator(File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static LineIterator |
lineIterator(File file,
String encoding)
Returns an Iterator for the lines in a
File. |
static FileInputStream |
openInputStream(File file)
Opens a
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file). |
static FileOutputStream |
openOutputStream(File file)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static FileOutputStream |
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)
Reads the contents of a file into a String using the default encoding for the VM.
|
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static List<String> |
readLines(File file)
Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
|
static List<String> |
readLines(File file,
String encoding)
Reads the contents of a file line by line to a List of Strings.
|
static void |
write(File file,
CharSequence data)
Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
|
static void |
write(File file,
CharSequence data,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist using the default encoding for the VM.
|
static void |
write(File file,
CharSequence data,
String encoding)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(File file,
CharSequence data,
String encoding,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist.
|
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 |
writeLines(File file,
Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
String lineEnding,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
String encoding,
Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
String encoding,
Collection<?> lines,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line, optionally appending. |
static void |
writeLines(File file,
String encoding,
Collection<?> lines,
String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
String encoding,
Collection<?> lines,
String lineEnding,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeStringToFile(File file,
String data)
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
|
static void |
writeStringToFile(File file,
String data,
boolean append)
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
|
static void |
writeStringToFile(File file,
String data,
String encoding)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(File file,
String data,
String encoding,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
public 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 errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static String readFileToString(File file) throws IOException
file - the file to read, must not be nullnullIOException - 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 List<String> readLines(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 errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static List<String> readLines(File file) throws IOException
file - the file to read, must not be nullnullIOException - in case of an I/O errorpublic static LineIterator lineIterator(File file, String encoding) throws IOException
File.
This method opens an InputStream for the file.
When you have finished with the iterator you should close the stream
to free internal resources. This can be done by calling the
LineIterator.close() or
LineIterator.closeQuietly(LineIterator) method.
The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
If an exception occurs during the creation of the iterator, the underlying stream is closed.
file - the file to open for input, must not be nullencoding - the encoding to use, null means platform defaultnullIOException - in case of an I/O error (file closed)public static LineIterator lineIterator(File file) throws IOException
File using the default encoding for the VM.file - the file to open for input, must not be nullnullIOException - in case of an I/O error (file closed)lineIterator(File, String)public 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, String 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 errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeStringToFile(File file, String data) throws IOException
file - 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, boolean append) throws IOException
file - the file to writedata - the content to write to the fileappend - 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 void write(File file, CharSequence data) throws IOException
file - the file to writedata - the content to write to the fileIOException - in case of an I/O errorpublic static void write(File file, CharSequence data, boolean append) throws IOException
file - the file to writedata - the content to write to the fileappend - if true, then the data will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static void write(File file, CharSequence 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 write(File file, CharSequence data, String 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 data will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic 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 errorpublic 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 writeLines(File file, String encoding, Collection<?> lines) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the default line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write toencoding - the encoding to use, null means platform defaultlines - the lines to write, null entries produce blank linesIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(File file, String encoding, Collection<?> lines, boolean append) throws IOException
toString() value of each item in a collection to
the specified File line by line, optionally appending.
The specified character encoding and the default line ending will be used.file - the file to write toencoding - the encoding to use, null means platform defaultlines - the lines to write, null entries produce blank linesappend - if true, then the lines will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(File file, Collection<?> lines) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the default line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank linesIOException - in case of an I/O errorpublic static void writeLines(File file, Collection<?> lines, boolean append) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the default line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank linesappend - if true, then the lines will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write toencoding - the encoding to use, null means platform defaultlines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding, boolean append) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The specified character encoding and the line ending will be used.file - the file to write toencoding - the encoding to use, null means platform defaultlines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultappend - if true, then the lines will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(File file, Collection<?> lines, String lineEnding) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the specified line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultIOException - in case of an I/O errorpublic static void writeLines(File file, Collection<?> lines, String lineEnding, boolean append) throws IOException
toString() value of each item in a collection to
the specified File line by line.
The default VM encoding and the specified line ending will be used.file - the file to write tolines - the lines to write, null entries produce blank lineslineEnding - the line separator to use, null is system defaultappend - if true, then the lines will be added to the
end of the file rather than overwritingIOException - in case of an I/O errorpublic static FileInputStream 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 nullFileInputStream for the specified fileFileNotFoundException - if the file does not existIOException - if the file object is a directoryIOException - if the file cannot be readpublic static FileOutputStream 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 nullFileOutputStream 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 FileOutputStream 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 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 void deleteDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion is unsuccessfulpublic static void cleanDirectory(File directory) throws IOException
directory - directory to cleanIOException - in case cleaning is unsuccessfulpublic 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 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 using FilenameUtils.isSystemWindows()
file - the file to checkIOException - if an IO error occurs while checking the filepublic 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 void forceDeleteOnExit(File file) throws IOException
file - file or directory to delete, must not be nullNullPointerException - if the file is nullIOException - in case deletion is unsuccessfulCopyright © 2021–2022. All rights reserved.