Package com.google.common.io
Class Closeables
- java.lang.Object
-
- com.google.common.io.Closeables
-
@Beta public final class Closeables extends java.lang.Object
Utility methods for working withCloseableobjects.- Since:
- 1.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidclose(java.io.Closeable closeable, boolean swallowIOException)Closes aCloseable, with control over whether anIOExceptionmay be thrown.static voidcloseQuietly(java.io.Closeable closeable)Deprecated.Where possible, use the try-with-resources statement if using JDK7 orCloseron JDK6 to close one or moreCloseableobjects.
-
-
-
Method Detail
-
close
public static void close(@Nullable java.io.Closeable closeable, boolean swallowIOException) throws java.io.IOExceptionCloses aCloseable, with control over whether anIOExceptionmay be thrown. This is primarily useful in a finally block, where a thrown exception needs to be logged but not propagated (otherwise the original exception will be lost).If
swallowIOExceptionis true then we never throwIOExceptionbut merely log it.Example:
public void useStreamNicely() throws IOException { SomeStream stream = new SomeStream("foo"); boolean threw = true; try { // ... code which does something with the stream ... threw = false; } finally { // If an exception occurs, rethrow it only if threw==false: Closeables.close(stream, threw); } }- Parameters:
closeable- theCloseableobject to be closed, or null, in which case this method does nothingswallowIOException- if true, don't propagate IO exceptions thrown by theclosemethods- Throws:
java.io.IOException- ifswallowIOExceptionis false andclosethrows anIOException.
-
closeQuietly
@Deprecated public static void closeQuietly(@Nullable java.io.Closeable closeable)Deprecated.Where possible, use the try-with-resources statement if using JDK7 orCloseron JDK6 to close one or moreCloseableobjects. This method is deprecated because it is easy to misuse and may swallow IO exceptions that really should be thrown and handled. See Guava issue 1118 for a more detailed explanation of the reasons for deprecation and see Closing Resources for more information on the problems with closingCloseableobjects and some of the preferred solutions for handling it correctly. This method is scheduled to be removed in Guava 16.0.Equivalent to callingclose(closeable, true), but with no IOException in the signature.- Parameters:
closeable- theCloseableobject to be closed, or null, in which case this method does nothing
-
-