---
title: 'Google Guava Closeables'
url: 'https://tutego.de/blog/javainsel/2014/01/google-guava-closeables'
markdown: 'https://tutego.de/blog/javainsel/2014/01/google-guava-closeables.md'
date: '2014-01-01'
description: 'To shorten things and not to repeat ourselves (usually the closing always looks the same) Google Commons offers the utility class com.google.common.io.Closeables. In this class you can find two static helper methods close(Closeable) and closeQuietly(Closeable). Both take an argument of type Closeab…'
taxonomy:
  category:
    - 'Open Source'
    - Guava
---

# Google Guava Closeables

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Google Guava Closeables

01.01.2014 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

To shorten things and not to repeat ourselves (usually the closing always looks the same) Google Commons offers the utility class `com.google.common.io.Closeables`. In this class you can find two static helper methods `close(Closeable)` and `closeQuietly(Closeable)`. Both take an argument of type `Closeable`—like a FileInputStream—and call the `close()` method on this object eventually. If the argument is `null`, nothing happens; in our hand-coded version we use an extra if (out != `null`) to prevent a `NullPointerException` from `out.close()` if out is `null`.

The following example uses `Closeable.closeQuietly(Closeable)` to close a stream. Any potential IOExceptions caused by `close()` is swallowed by `closeQuietly()`.

```java
package com.tutego.googlecommon.io;

import java.io.*;

import java.util.logging.*;

import com.google.common.io.*;

public class CloseablesDemo {

private static final Logger log = Logger.getLogger( CloseablesDemo.class.getName() );

public static void main( String[] args ) {

InputStream in = null;

try {

in = Resources.getResource( CloseablesDemo.class, "test.txt" ).openStream();

BufferedReader br = new BufferedReader( new InputStreamReader( in, "utf-8" ) );

System.out.println( br.readLine() );

} catch ( IOException e ) {

log.log( Level.SEVERE, "IOException thrown while reading line", e );

} finally {

Closeables.closeQuietly( in );

}

}

}
```

Using `closeQuietly()` shortens a program but it does not notify about any exception caused by the inner `close()` method. A lot of programmers ignore this exception and shortens there closing block to

```
try { out.close(); } catch ( Exception e ) {}
```

This style is dangerous, usually not for readers but for all modifying writers. If a writing stream can’t be closed the `IOException` shows a severe problem that probably the output is not complete and data is missing. For that reason the utility class `Closeable` offers a second message, `close()` which, in contrast to `closeQuietly()` first is denoted by an `IOException` clause and secondly controls with a boolean parameter if in case of an `IOException` caused by `close()` this exception should be swallowed or rethrown.

To summarize these methods:

```java
class Closeables

static void close(Closeable closeable, boolean swallowIOException) throws IOException
```

Closes the closeable if it is not `null`. The boolean parameter controls whether an exception will be rethrown (pass `false` for `swallowIOException`) or swallowed (pass `true`).

```java
static void closeQuietly Closeable closeable)
```

If closeable is not `null` this method calls `closeable.close()`. If `close()` throws an `IOException` this exception is swallowed by `closeQuietly()`.Because `closeQuietly(closeable)` swallows the exception it is internally written as `close(closeable, true)`.

Kategorien: [Open Source](https://tutego.de/blog/javainsel/category/open-source/), [Guava](https://tutego.de/blog/javainsel/category/guava/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Guava Version 16 auf dem Weg](https://tutego.de/blog/javainsel/2014/01/guava-version-16-auf-dem-weg-2.md)
- Next: [Für Microsoft ist Java performanter als C# …](https://tutego.de/blog/javainsel/2014/01/fr-microsoft-ist-java-performanter-als-c.md)
