public class FileIOUtils extends Object
Constructor and Description |
---|
FileIOUtils() |
Modifier and Type | Method and Description |
---|---|
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 byte[] |
readAllBytes(Path path)
Reads all the bytes from a file.
|
static String |
readFile(File file,
String charsetName) |
static String |
readFileUtf8(File file) |
static void |
writeCompletely(WritableByteChannel channel,
ByteBuffer src) |
static void |
writeFile(File file,
String contents,
String encoding) |
static void |
writeFileUtf8(File file,
String contents) |
public static void writeCompletely(WritableByteChannel channel, ByteBuffer src) throws IOException
IOException
public 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 byte[] readAllBytes(Path path) throws IOException
This is an implementation that follow Files.readAllBytes(java.nio.file.Path)
, and the difference is that it limits
the size of the direct buffer to avoid direct-buffer OutOfMemoryError. When Files.readAllBytes(java.nio.file.Path)
or other interfaces in java API can do
this in the future, we should remove it.
path
- the path to the fileIOException
- if an I/O error occurs reading from the streamOutOfMemoryError
- if an array of the required size cannot be allocated, for example
the file is larger that 2GB
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.Copyright © 2023–2024 The Apache Software Foundation. All rights reserved.