The following are 3 different ways to close a output writer. the first one puts close () method in try clause, the second one puts close in finally clause, and the third one uses a try-with-resources statement. which one is the right or the best?
= FileWriter("out.txt", "the text"
PrintWriter out = = FileWriter("out.txt", "the text" (out !=
(PrintWriter out2 = FileWriter("out.txt", "the text"
Answer
Because the Writer shocould be closed in either case (exception or no exception), close () shocould be put in finally clause.
From Java 7, we can useTry-with-resourcesStatement. Why?
The try-with-resources Statement
Thetry
-With-resources statement istry
Statement that declares one or more resources.ResourceIs an object that must be closed after the program is finished with it.try
-With-resources statement ensures that each resource is closed at the end of the statement. Any object that implementsjava.lang.AutoCloseable
, Which includes all objects which implementjava.io.Closeable
, Can be used as a resource.
The following example reads the first line from a file. It uses an instanceBufferedReader
To read data from the file.BufferedReader
Is a resource that must be closed after the program is finished with it:
static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); }}
In this example, the resource declared intry
-With-resources statement isBufferedReader
. The declaration statement appears within parentheses immediately aftertry
Keyword. The classBufferedReader
, In Java SE 7 and later, implements the interfacejava.lang.AutoCloseable
. BecauseBufferedReader
Instance is declared intry
-With-resource statement, it will be closed regardless of whethertry
Statement completes normally or abruptly (as a result of the methodBufferedReader.readLine
ThrowingIOException
).
Prior to Java SE 7, you can usefinally
Block to ensure that a resource is closed regardless of whethertry
Statement completes normally or abruptly. The following example usesfinally
Block instead oftry
-With-resources statement:
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { if (br != null) br.close(); }}
However, in this example, if the methodsreadLine
Andclose
Both throw exceptions, then the methodreadFirstLineFromFileWithFinallyBlock
Throws the exception thrown fromfinally
Block; the exception thrown fromtry
Block is suppressed. In contrast, in the examplereadFirstLineFromFile
, If exceptions are thrown from bothtry
Block andtry
-With-resources statement, then the methodreadFirstLineFromFile
Throws the exception thrown fromtry
Block; the exception thrown fromtry
-With-resources block is suppressed. In Java SE 7 and later, you can retrieve suppressed limits; see the section Suppressed limits for more information.
You may declare one or more resources intry
-With-resources statement. The following example retrieves the names of the files packaged in the zip filezipFileName
And creates a text file that contains the names of these files:
public static void writeToFileZipFileContents(String zipFileName, String outputFileName) throws java.io.IOException { java.nio.charset.Charset charset = java.nio.charset.StandardCharsets.US_ASCII; java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName); // Open zip file and create output file with // try-with-resources statement try ( java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) ) { // Enumerate each entry for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) { // Get the entry name and write it to the output file String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine; writer.write(zipEntryName, 0, zipEntryName.length()); } }}
In this example,try
-With-resources statement contains two declarations that are separated by a semicolon:ZipFile
AndBufferedWriter
. When the block of code that directly follows it terminates, either normally or because of an exception,close
Methods ofBufferedWriter
AndZipFile
Objects are automatically called in this order. Note thatclose
Methods of resources are called inOppositeOrder of their creation.
The following example usestry
-With-resources statement to automatically closejava.sql.Statement
Object:
public static void viewTable(Connection con) throws SQLException { String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES"; try (Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String coffeeName = rs.getString("COF_NAME"); int supplierID = rs.getInt("SUP_ID"); float price = rs.getFloat("PRICE"); int sales = rs.getInt("SALES"); int total = rs.getInt("TOTAL"); System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); }}
The resourcejava.sql.Statement
Used in this example is part of the JDBC 4.1 and later API.
Note:try
-With-resources statement can havecatch
Andfinally
Blocks just like an ordinarytry
Statement. Intry
-With-resources statement, anycatch
Orfinally
Block is run after the resources declared have been closed.
Suppressed Exceptions
An exception can be thrown from the block of code associated withtry
-With-resources statement. In the examplewriteToFileZipFileContents
, An exception can be thrown fromtry
Block, and up to two exceptions can be thrown fromtry
-With-resources statement when it tries to closeZipFile
AndBufferedWriter
Objects. If an exception is thrown fromtry
Block and one or more exceptions are thrown fromtry
-With-resources statement, then those exceptions thrown fromtry
-With-resources statement are suppressed, and the exception thrown by the block is the one that is thrown bywriteToFileZipFileContents
Method. You can retrieve these suppressed tions by callingThrowable.getSuppressed
Method from the exception thrown bytry
Block.
Classes That Implement the AutoCloseable or Closeable Interface
See the Javadoc ofAutoCloseable
AndCloseable
Interfaces for a list of classes that implement either of these interfaces.Closeable
Interface extendsAutoCloseable
Interface.close
Method ofCloseable
Interface throws exceptions of typeIOException
Whileclose
Method ofAutoCloseable
Interface throws exceptions of typeException
. Consequently, subclasses ofAutoCloseable
Interface can override this behavior ofclose
Method to throw specialized exceptions, suchIOException
, Or no exception at all.