public final class FileUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static char |
SYSTEM_SEPARATOR
The system separator character.
|
static char |
UNIX_SEPARATOR
The Unix separator character.
|
static char |
WINDOWS_SEPARATOR
The Windows separator character.
|
| Modifier and Type | Method and Description |
|---|---|
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 File |
getTempDirectory()
Returns a
File representing the system temporary directory. |
static String |
getTempDirectoryPath()
Returns the path to the system temporary directory.
|
static File |
getUserDirectory()
Returns a
File representing the user's home directory. |
static String |
getUserDirectoryPath()
Returns the path to the user's home directory.
|
static boolean |
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.
|
static FileInputStream |
openInputStream(File file)
Opens a
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file). |
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 List<String> |
readLines(InputStream input)
Get the contents of an
InputStream as a list of Strings,
one entry per line, using the default character encoding of the platform. |
static List<String> |
readLines(InputStream input,
String encoding)
Get the contents of an
InputStream as a list of Strings,
one entry per line, using the specified character encoding. |
static List<String> |
readLines(Reader input)
Get the contents of a
Reader as a list of Strings,
one entry per line. |
public static final char UNIX_SEPARATOR
public static final char WINDOWS_SEPARATOR
public static final char SYSTEM_SEPARATOR
public static String getTempDirectoryPath()
public static File getTempDirectory()
File representing the system temporary directory.public static String getUserDirectoryPath()
public static File getUserDirectory()
File representing the user's home directory.public static void deleteDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion 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 void cleanDirectory(File directory) throws IOException
directory - directory to cleanIOException - in case cleaning 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 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 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.
file - the file to checkIOException - if an IO error occurs while checking the filepublic 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 List<String> readLines(InputStream input) throws IOException
InputStream as a list of Strings,
one entry per line, using the default character encoding of the platform.
This method buffers the input internally, so there is no need to use a BufferedInputStream.
input - the InputStream to read from, not nullNullPointerException - if the input is nullIOException - if an I/O error occurspublic static List<String> readLines(InputStream input, String encoding) throws IOException
InputStream as a list of Strings,
one entry per line, using the specified character encoding.
Character encoding names can be found at IANA.
This method buffers the input internally, so there is no need to use a BufferedInputStream.
input - the InputStream to read from, not nullencoding - the encoding to use, null means platform defaultNullPointerException - if the input is nullIOException - if an I/O error occurspublic static List<String> readLines(Reader input) throws IOException
Reader as a list of Strings,
one entry per line.
This method buffers the input internally, so there is no need to use a BufferedReader.
input - the Reader to read from, not nullNullPointerException - if the input is nullIOException - if an I/O error occurspublic 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 readCopyright © 2018. All Rights Reserved.