test/java/nio/channels/FileChannel/Truncate.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -56,14 +56,15 @@
      */
     static void basicTest(File blah) throws Exception {
         for(int i=0; i<100; i++) {
             long testSize = generator.nextInt(1000) + 10;
             initTestFile(blah, testSize);
-            FileChannel fc = (i < 50) ?
+
+            try (FileChannel fc = (i < 50) ?
                 new RandomAccessFile(blah, "rw").getChannel() :
-                FileChannel.open(blah.toPath(), READ, WRITE);
-            try (fc) {
+                 FileChannel.open(blah.toPath(), READ, WRITE))
+                {
                 if (fc.size() != testSize)
                     throw new RuntimeException("Size failed");
 
                 long position = generator.nextInt((int)testSize);
                 fc.position(position);

@@ -90,14 +91,14 @@
      */
     static void appendTest(File blah) throws Exception {
         for (int i=0; i<10; i++) {
             long testSize = generator.nextInt(1000) + 10;
             initTestFile(blah, testSize);
-            FileChannel fc = (i < 5) ?
+            try (FileChannel fc = (i < 5) ?
                 new FileOutputStream(blah, true).getChannel() :
-                FileChannel.open(blah.toPath(), APPEND);
-            try (fc) {
+                 FileChannel.open(blah.toPath(), APPEND))
+                {
                 // truncate file
                 long newSize = generator.nextInt((int)testSize);
                 fc.truncate(newSize);
                 if (fc.size() != newSize)
                     throw new RuntimeException("Truncate failed");