jdk/test/java/io/RandomAccessFile/ReadLine.java
changeset 5810 e83d67ad8c96
parent 5506 202f599c92aa
child 7668 d4a77089c587
--- a/jdk/test/java/io/RandomAccessFile/ReadLine.java	Fri Jun 18 20:59:13 2010 +0100
+++ b/jdk/test/java/io/RandomAccessFile/ReadLine.java	Sat Jun 19 15:17:36 2010 +0100
@@ -33,26 +33,30 @@
     public static void main(String args[]) throws Exception {
         File fn = new File("x.ReadLine");
         RandomAccessFile raf = new RandomAccessFile(fn,"rw");
-        String line;
-        int ctr = 1;
-        String expected;
+        try {
+            String line;
+            int ctr = 1;
+            String expected;
 
-        raf.writeBytes
-            ("ln1\rln2\r\nln3\nln4\rln5\r\nln6\n\rln8\r\rln10\n\nln12\r\r\nln14");
-        raf.seek(0);
+            raf.writeBytes
+                ("ln1\rln2\r\nln3\nln4\rln5\r\nln6\n\rln8\r\rln10\n\nln12\r\r\nln14");
+            raf.seek(0);
 
-        while ((line=raf.readLine()) != null) {
-            if ((ctr == 7) || (ctr == 9) ||
-                (ctr == 11) || (ctr == 13)) {
-                expected = "";
-            } else {
-                expected = "ln" + ctr;
+            while ((line=raf.readLine()) != null) {
+                if ((ctr == 7) || (ctr == 9) ||
+                    (ctr == 11) || (ctr == 13)) {
+                     expected = "";
+                } else {
+                    expected = "ln" + ctr;
+                }
+                if (!line.equals(expected)) {
+                    throw new Exception("Expected \"" + expected + "\"" +
+                                        ", read \"" + line + "\"");
+                }
+                ctr++;
             }
-            if (!line.equals(expected)) {
-                throw new Exception("Expected \"" + expected + "\"" +
-                                    ", read \"" + line + "\"");
-            }
-            ctr++;
+        } finally {
+            raf.close();
         }
         System.err.println("Successfully completed test!");
     }