Java 7 enhanced try statement closes resources
Traditional ways of closing resources
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; class Student implements Serializable { private String name; public Student(String name) { this.name = name; } } public class test2 { public static void main(String[] args) throws Exception { Student s = new Student("WJY"); Student s2 = null; ObjectOutputStream oos = null; ObjectInputStream ois = null; try { //Create object output stream oos = new ObjectOutputStream(new FileOutputStream("b.bin")); //Create object input stream ois = new ObjectInputStream(new FileInputStream("b.bin")); //Serialize java objects oos.writeObject(s); oos.flush(); //Deserializing java objects s2 = (Student) ois.readObject(); } finally { //Recycling resources using final blocks if (oos != null) { try { oos.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (ois != null) { try { ois.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } }
- Use final blocks to close physical resources to ensure that closing operations are always performed.
- Before closing each resource, make sure that the reference variable that references that resource is not null.
-
Use a separate try...catch block for each physical resource to close the resource, ensuring that the exception thrown when closing the resource does not affect the closure of other resources.
The above approach leads to bloated final block code and reduced readability of the program.
Java 7 enhanced try statement closes resources
In order to solve the problem of the traditional way, Java 7 adds a try statement that automatically closes resources. It allows one or more resources to be declared and initialized in parentheses immediately after the try keyword. The resources here refer to those resources that must be displayed at the end of the program (database connection, network connection, etc.). The try statement automatically closes these resources at the end of the statement.
public class test2 { public static void main(String[] args) throws Exception { Student s = new Student("WJY"); Student s2 = null; try (//Create object output stream ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.bin")); //Create object input stream ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.bin")); ) { //Serialize java objects oos.writeObject(s); oos.flush(); //Deserializing java objects s2 = (Student) ois.readObject(); } } }
The try statement that automatically closes a resource contains an implicit final block (for closing the resource), so the try statement can have neither a catch block nor a final block.
Be careful:
- The resources that are automatically closed must implement Closeable or AutoCloseable interfaces. (Closeable is a subinterface of AutoCloseable, the close() method declaration in Closeeable throws IOException, and the close() method declaration in AutoCloseable throws Exception.)
- Closed resources must be declared and initialized in parentheses after the try statement. If a program has a try statement that needs to shut down resources automatically, it can take more than one catch block and one final block.
Java 7 has almost all the "resource classes" (including various classes of file IO, Connection, Statement and other interfaces of JDBC programming...) The rewritten resource classes implement either AutoCloseable or Closeable interfaces