public final class IOUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Charset |
CHARSET_UTF_8
Deprecated.
Use
StandardCharsets.UTF_8 instead. |
static String |
UTF_8
UTF-8 charset string.
|
Modifier and Type | Method and Description |
---|---|
static void |
close(Closeable... objects)
Closes all given Closeables.
|
static void |
close(Iterable<? extends Closeable> objects)
Closes all given Closeables.
|
static void |
closeWhileHandlingException(Closeable... objects)
Closes all given Closeables, suppressing all thrown exceptions.
|
static void |
closeWhileHandlingException(Iterable<? extends Closeable> objects)
Closes all given Closeables, suppressing all thrown exceptions.
|
static void |
copy(File source,
File target)
Copy one file's contents to another file.
|
static void |
deleteFilesIgnoringExceptions(Directory dir,
String... files)
Deletes all given files, suppressing all thrown IOExceptions.
|
static void |
fsync(File fileToSync,
boolean isDir)
Ensure that any writes to the given file is written to the storage device that contains it.
|
static Reader |
getDecodingReader(Class<?> clazz,
String resource,
Charset charSet)
Opens a Reader for the given resource using a
CharsetDecoder . |
static Reader |
getDecodingReader(File file,
Charset charSet)
Opens a Reader for the given
File using a CharsetDecoder . |
static Reader |
getDecodingReader(InputStream stream,
Charset charSet)
Wrapping the given
InputStream in a reader using a CharsetDecoder . |
static void |
reThrow(Throwable th)
Simple utilty method that takes a previously caught
Throwable and rethrows either IOException or an unchecked exception. |
static void |
reThrowUnchecked(Throwable th)
Simple utilty method that takes a previously caught
Throwable and rethrows it as an unchecked exception. |
@Deprecated public static final Charset CHARSET_UTF_8
StandardCharsets.UTF_8
instead.Charset
instance to prevent repeated
Charset.forName(String)
lookupspublic static final String UTF_8
Where possible, use StandardCharsets.UTF_8
instead,
as using the String constant may slow things down.
StandardCharsets.UTF_8
public static void close(Closeable... objects) throws IOException
objects
- objects to call close() onIOException
public static void close(Iterable<? extends Closeable> objects) throws IOException
IOException
close(Closeable...)
public static void closeWhileHandlingException(Closeable... objects)
objects
- objects to call close() onpublic static void closeWhileHandlingException(Iterable<? extends Closeable> objects)
public static Reader getDecodingReader(InputStream stream, Charset charSet)
InputStream
in a reader using a CharsetDecoder
.
Unlike Java's defaults this reader will throw an exception if your it detects
the read charset doesn't match the expected Charset
.
Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, its not recommended to use as a common purpose reader.
stream
- the stream to wrap in a readercharSet
- the expected charsetpublic static Reader getDecodingReader(File file, Charset charSet) throws IOException
File
using a CharsetDecoder
.
Unlike Java's defaults this reader will throw an exception if your it detects
the read charset doesn't match the expected Charset
.
Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, its not recommended to use as a common purpose reader.
file
- the file to open a reader oncharSet
- the expected charsetIOException
public static Reader getDecodingReader(Class<?> clazz, String resource, Charset charSet) throws IOException
CharsetDecoder
.
Unlike Java's defaults this reader will throw an exception if your it detects
the read charset doesn't match the expected Charset
.
Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, its not recommended to use as a common purpose reader.
clazz
- the class used to locate the resourceresource
- the resource name to loadcharSet
- the expected charsetIOException
public static void deleteFilesIgnoringExceptions(Directory dir, String... files)
Note that the files should not be null.
public static void copy(File source, File target) throws IOException
IOException
public static void reThrow(Throwable th) throws IOException
Throwable
and rethrows either IOException
or an unchecked exception. If the
argument is null then this method does nothing.IOException
public static void reThrowUnchecked(Throwable th)
Throwable
and rethrows it as an unchecked exception.
If the argument is null then this method does nothing.public static void fsync(File fileToSync, boolean isDir) throws IOException
fileToSync
- the file to fsyncisDir
- if true, the given file is a directory (we open for read and ignore IOExceptions,
because not all file systems and operating systems allow to fsync on a directory)IOException
Copyright © 2000-2014 Apache Software Foundation. All Rights Reserved.