Java Utililty Methods Stream Close

List of utility methods to do Stream Close

Description

The list of methods to do Stream Close are organized into topic(s).

Method

Throwableclose(final OutputStream dest)
close
if (dest != null) {
    try {
        dest.close();
    } catch (final Throwable t) {
        return t;
return null;
...
voidclose(final Scanner scanner)
close
if (scanner != null) {
    scanner.close();
voidclose(Iterator iterator)
Checks if the provided iterator implements Closeable .
if (iterator != null && iterator instanceof Closeable) {
    try {
        ((Closeable) iterator).close();
    } catch (IOException e) {
        String name = iterator.getClass().getPackage().toString();
        Logger log = Logger.getLogger(name);
        log.log(Level.FINE, e.getMessage(), e);
voidclose(java.io.Closeable in)
Closes a stream, handling all exceptions
if (in == null)
    return;
try {
    in.close();
} catch (Exception e) {
voidclose(JMXConnector connector)
close
if (connector != null) {
    try {
        connector.close();
    } catch (IOException e) {
        e.printStackTrace();
voidclose(Object obj)
close
if (obj == null)
    return;
try {
    if (obj instanceof InputStream)
        ((InputStream) obj).close();
    else if (obj instanceof ZipFile)
        ((ZipFile) obj).close();
    else if (obj instanceof OutputStream)
...
voidclose(Object resource)
Closes the streams and resources.
try {
    if (resource instanceof InputStream) {
        ((InputStream) resource).close();
    } else if (resource instanceof OutputStream) {
        ((OutputStream) resource).close();
    } else if (resource instanceof Reader) {
        ((Reader) resource).close();
    } else if (resource instanceof Writer) {
...
voidclose(ObjectInput in)
close
try {
    if (in != null) {
        in.close();
} catch (Exception ex) {
voidclose(OutputStream in)
Just a convenience method to avoid littering code with if's and catches
try {
    if (in != null) {
        in.close();
} catch (Exception e) {
    e.printStackTrace();
voidclose(OutputStream os)
Close output streams that are not print streams.
if (!(os instanceof PrintStream)) {
    os.close();