Java 7 - try-with-resources statement example

Posted on January 4, 2017


The try-with-resources statement is a new try-statement for handling one or more resources, was introduced in Java SE 7. A resource is a object that must be closed after the program is finished with it.

The try-with-resources statement automatically closes all resources, which implements java.lang.AutoCloseable interface, at end of the try statement.

The following examples demonstrate usages of the try-catch-finally and try-with-resources statements.

Classical example of try-catch-finally statement

In Java SE 6 or earlier version, we use try-catch-finally statement for resources management as shown below.

TryCatachFinallyExample.java

package com.boraji.tutorial;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * @author imssbora
 */
public class TryCatachFinallyExample {
  public static void main(String[] args) {
    File file = new File("file.txt");

    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      while (line != null) {
        System.out.println(line);
        line = reader.readLine();
      }
    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
      try {
        if (reader != null) {
          reader.close();
        }
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }

  }
}

The try-with-resources statement example

Here is an example of try-with-resources statement, which closes the BufferedReader stream automatically.

TryWithResourcesExample.java

package com.boraji.tutorial;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author imssbora
 */
public class TryWithResourcesExample {
  public static void main(String[] args) throws IOException {
    File file = new File("file.txt");
    try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
      String line = reader.readLine();
      while (line != null) {
        System.out.println(line);
        line = reader.readLine();
      }
    }
  }
}

 

The try-with-resources statement example with multiple resources

The following example shows how to declare multiple resources (BufferedReader  and BufferedWriter streams ) within parentheses immediately after the try keyword.

TryWithResourcesExample.java

package com.boraji.tutorial;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author imssbora
 */
public class TryWithResourcesExample {
  public static void main(String[] args) throws IOException {
    File inFile = new File("file.txt");
    File outFile = new File("file2.txt");
    try (
        BufferedReader reader = new BufferedReader(new FileReader(inFile)); 
        BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
       ) {
      String line = reader.readLine();
      while (line != null) {
        writer.write(line);
        line = reader.readLine();
      }
    }
  }
}

The try-with-resources statement example with catch block

Here is an example of using catch block with try-with-resources statement.

TryWithResourcesExample.java

package com.boraji.tutorial;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author imssbora
 */
public class TryWithResourcesExample {
  public static void main(String[] args) {
    File file = new File("file.txt");
    try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
      String line = reader.readLine();
      while (line != null) {
        System.out.println(line);
        line = reader.readLine();
      }
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
  }
}