org.apache.lucene.util

## Class IOUtils

- java.lang.Object
- - org.apache.lucene.util.IOUtils

    public final class IOUtils
    extends Object

This class emulates the new Java 7 "Try-With-Resources" statement. Remove once Lucene is on Java 7.

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

### Field Summary

Fields
`static Charset` `CHARSET_UTF_8` UTF-8 `Charset` instance to prevent repeated `Charset.forName(String)` lookups
`static String` `UTF_8` UTF-8 charset string

### Method Summary

Methods
`static void` `close(Closeable... objects)` Closes all given `Closeable`s.
`static void` `close(Iterable<? extends Closeable> objects)` Closes all given `Closeable`s.
`static void` `closeWhileHandlingException(Closeable... objects)` Closes all given `Closeable`s, suppressing all thrown exceptions.
`static <E extends Exception> void` `closeWhileHandlingException(E priorException, Closeable... objects)` Closes all given `Closeable`s, suppressing all thrown exceptions.
`static <E extends Exception> void` `closeWhileHandlingException(E priorException, Iterable<? extends Closeable> objects)` Closes all given `Closeable`s, suppressing all thrown exceptions.
`static void` `closeWhileHandlingException(Iterable<? extends Closeable> objects)` Closes all given `Closeable`s, 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 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`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### UTF_8

    public static final String UTF_8

UTF-8 charset string

See Also:
  `Charset.forName(String)`, Constant Field Values

#### CHARSET_UTF_8

    public static final Charset CHARSET_UTF_8

UTF-8 `Charset` instance to prevent repeated `Charset.forName(String)` lookups

### Method Detail

#### closeWhileHandlingException

    public static <E extends Exception> void closeWhileHandlingException(E priorException,
                                                         Closeable... objects)
                                            throws E extends Exception,
                                                   IOException

Closes all given `Closeable`s, suppressing all thrown exceptions. Some of the `Closeable`s may be null, they are ignored. After everything is closed, method either throws `priorException`, if one is supplied, or the first of suppressed exceptions, or completes normally.

Sample usage:

     Closeable resource1 = null, resource2 = null, resource3 = null;
     ExpectedException priorE = null;
     try {
       resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
       ..do..stuff.. // May throw ExpectedException
     } catch (ExpectedException e) {
       priorE = e;
     } finally {
       closeWhileHandlingException(priorE, resource1, resource2, resource3);
     }

Parameters:
  `priorException` - `null` or an exception that will be rethrown after method completion
  `objects` - objects to call `close()` on
Throws:
  `E extends Exception`
  `IOException`

#### closeWhileHandlingException

    public static <E extends Exception> void closeWhileHandlingException(E priorException,
                                                         Iterable<? extends Closeable> objects)
                                            throws E extends Exception,
                                                   IOException

Closes all given `Closeable`s, suppressing all thrown exceptions.

Throws:
  `E extends Exception`
  `IOException`
See Also:
  `closeWhileHandlingException(Exception, Closeable...)`

#### close

    public static void close(Closeable... objects)
                      throws IOException

Closes all given `Closeable`s. Some of the `Closeable`s may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing, or completes normally if there were no exceptions.

Parameters:
  `objects` - objects to call `close()` on
Throws:
  `IOException`

#### close

    public static void close(Iterable<? extends Closeable> objects)
                      throws IOException

Closes all given `Closeable`s.

Throws:
  `IOException`
See Also:
  `close(Closeable...)`

#### closeWhileHandlingException

    public static void closeWhileHandlingException(Closeable... objects)

Closes all given `Closeable`s, suppressing all thrown exceptions. Some of the `Closeable`s may be null, they are ignored.

Parameters:
  `objects` - objects to call `close()` on

#### closeWhileHandlingException

    public static void closeWhileHandlingException(Iterable<? extends Closeable> objects)

Closes all given `Closeable`s, suppressing all thrown exceptions.

See Also:
  `closeWhileHandlingException(Closeable...)`

#### getDecodingReader

    public static Reader getDecodingReader(InputStream stream,
                           Charset charSet)

Wrapping the given `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.

Parameters:
  `stream` - the stream to wrap in a reader
  `charSet` - the expected charset
Returns:
  a wrapping reader

#### getDecodingReader

    public static Reader getDecodingReader(File file,
                           Charset charSet)
                                    throws IOException

Opens a Reader for the given `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.

Parameters:
  `file` - the file to open a reader on
  `charSet` - the expected charset
Returns:
  a reader to read the given file
Throws:
  `IOException`

#### getDecodingReader

    public static Reader getDecodingReader(Class<?> clazz,
                           String resource,
                           Charset charSet)
                                    throws IOException

Opens a Reader for the given resource 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.

Parameters:
  `clazz` - the class used to locate the resource
  `resource` - the resource name to load
  `charSet` - the expected charset
Returns:
  a reader to read the given file
Throws:
  `IOException`

#### deleteFilesIgnoringExceptions

    public static void deleteFilesIgnoringExceptions(Directory dir,
                                     String... files)

Deletes all given files, suppressing all thrown IOExceptions.

Note that the files should not be null.

#### copy

    public static void copy(File source,
            File target)
                     throws IOException

Copy one file's contents to another file. The target will be overwritten if it exists. The source must exist.

Throws:
  `IOException`