public final class FileUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(File directory)
Removes all files contained within a directory, without removing the directory itself.
|
static void |
deleteDirectory(File directory)
Deletes the given directory recursively.
|
static void |
deleteDirectoryQuietly(File directory)
Deletes the given directory recursively, not reporting any I/O exceptions
that occur.
|
static void |
deleteFileOrDirectory(File file)
Removes the given file or directory recursively.
|
static boolean |
deletePathIfEmpty(FileSystem fileSystem,
Path path)
Deletes the path if it is empty.
|
static String |
getRandomFilename(String prefix)
Constructs a random filename with the given prefix and
a random part generated from hex characters.
|
static String |
readFile(File file,
String charsetName) |
static String |
readFileUtf8(File file) |
static void |
writeFile(File file,
String contents,
String encoding) |
static void |
writeFileUtf8(File file,
String contents) |
public static String getRandomFilename(String prefix)
prefix
- the prefix to the filename to be constructedpublic static String readFile(File file, String charsetName) throws IOException
IOException
public static String readFileUtf8(File file) throws IOException
IOException
public static void writeFile(File file, String contents, String encoding) throws IOException
IOException
public static void writeFileUtf8(File file, String contents) throws IOException
IOException
public static void deleteFileOrDirectory(File file) throws IOException
If the file or directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a file-to-be-deleted is not present a success.
This method is safe against other concurrent deletion attempts.
file
- The file or directory to delete.IOException
- Thrown if the directory could not be cleaned for some reason, for example
due to missing access/write permissions.public static void deleteDirectory(File directory) throws IOException
If the directory does not exist, this does not throw an exception, but simply does nothing. It considers the fact that a directory-to-be-deleted is not present a success.
This method is safe against other concurrent deletion attempts.
directory
- The directory to be deleted.IOException
- Thrown if the given file is not a directory, or if the directory could not be
deleted for some reason, for example due to missing access/write permissions.public static void deleteDirectoryQuietly(File directory)
This method is identical to deleteDirectory(File)
, except that it
swallows all exceptions and may leave the job quietly incomplete.
directory
- The directory to delete.public static void cleanDirectory(File directory) throws IOException
This method is safe against other concurrent deletion attempts.
directory
- The directory to remove all files from.FileNotFoundException
- Thrown if the directory itself does not exist.IOException
- Thrown if the file indicates a proper file and not a directory, or if
the directory could not be cleaned for some reason, for example
due to missing access/write permissions.public static boolean deletePathIfEmpty(FileSystem fileSystem, Path path) throws IOException
fileSystem
- to usepath
- to be deleted if emptyIOException
- if the delete operation failsCopyright © 2014–2018 The Apache Software Foundation. All rights reserved.