Para a especificação, consulte http://blogs.sun.com/darcy/entry/project_coin_updated_arm_spec
Exemplo:
import java.io.*;
class TesteTryWithResources {
public static void main (String[] args) {
// Exemplo simples
try (PrintWriter pw = new PrintWriter ("test.txt")) {
pw.println ("Hello, world!");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
// Exemplo um pouco mais complexo, que
// envolve 2 arquivos
try (BufferedOutputStream bos = new BufferedOutputStream (
new FileOutputStream ("copy.of.file"));
BufferedInputStream bis = new BufferedInputStream (
new FileInputStream ("original.file"))) {
byte[] buffer = new byte[10240];
for (int nBytes = bis.read (buffer); nBytes > 0;
nBytes = bis.read (buffer)) {
bos.write (buffer, 0, nBytes);
}
} catch (final FileNotFoundException | IOException ex) {
ex.printStackTrace();
}
}
}