test/java/lang/Runtime/shutdown/ShutdownHooks.java

Print this page
rev 3509 : 7021209: convert lang, math, util to use try-with-resources
Reviewed-by: XXX

@@ -41,27 +41,25 @@
 
         File dir = new File(args[0]);
         file = new File(dir, args[1]);
         // write to file
         System.out.println("writing to "+ file);
-        PrintWriter pw = new PrintWriter(file);
+        try (PrintWriter pw = new PrintWriter(file)) {
         pw.println("Shutdown begins");
-        pw.close();
+        }
     }
 
     public static class Cleaner extends Thread {
         public void run() {
             // register the Console's shutdown hook while the application
             // shutdown hook is running
             Console cons = System.console();
             // register the DeleteOnExitHook while the application
             // shutdown hook is running
             file.deleteOnExit();
-            try {
-                PrintWriter pw = new PrintWriter(file);
+            try (PrintWriter pw = new PrintWriter(file)) {
                 pw.println("file is being deleted");
-                pw.close();
             } catch (FileNotFoundException e) {
                 throw new RuntimeException(e);
             }
         }
     }