@@ -138,14 +138,12 @@ public void testThreeLines() throws Exception {
138138 public void testMissingFile () throws Exception {
139139 final File testFile = new File (temporaryFolder , "dummy-missing-file.txt" );
140140
141- LineIterator iterator = null ;
142- try {
143- iterator = FileUtils . lineIterator ( testFile , "UTF-8" );
141+ try (
142+ LineIterator iterator = FileUtils . lineIterator ( testFile , "UTF-8" );
143+ ){
144144 fail ("Expected FileNotFoundException" );
145145 } catch (final FileNotFoundException expected ) {
146146 // ignore, expected result
147- } finally {
148- LineIterator .closeQuietly (iterator );
149147 }
150148 }
151149
@@ -156,16 +154,15 @@ public void testValidEncoding() throws Exception {
156154 final File testFile = new File (temporaryFolder , "LineIterator-validEncoding.txt" );
157155 createLinesFile (testFile , encoding , 3 );
158156
159- final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
160- try {
157+ try (
158+ final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
159+ ){
161160 int count = 0 ;
162161 while (iterator .hasNext ()) {
163162 assertNotNull (iterator .next ());
164163 count ++;
165164 }
166165 assertEquals (3 , count );
167- } finally {
168- LineIterator .closeQuietly (iterator );
169166 }
170167 }
171168
@@ -176,14 +173,12 @@ public void testInvalidEncoding() throws Exception {
176173 final File testFile = new File (temporaryFolder , "LineIterator-invalidEncoding.txt" );
177174 createLinesFile (testFile , "UTF-8" , 3 );
178175
179- LineIterator iterator = null ;
180- try {
181- iterator = FileUtils . lineIterator ( testFile , encoding );
176+ try (
177+ LineIterator iterator = FileUtils . lineIterator ( testFile , encoding );
178+ ) {
182179 fail ("Expected UnsupportedCharsetException" );
183180 } catch (final UnsupportedCharsetException expected ) {
184181 // ignore, expected result
185- } finally {
186- LineIterator .closeQuietly (iterator );
187182 }
188183 }
189184
@@ -225,15 +220,14 @@ public void testNextOnly() throws Exception {
225220 final File testFile = new File (temporaryFolder , "LineIterator-nextOnly.txt" );
226221 final List <String > lines = createLinesFile (testFile , encoding , 3 );
227222
228- final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
229- try {
223+ try (
224+ final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
225+ ){
230226 for (int i = 0 ; i < lines .size (); i ++) {
231227 final String line = iterator .next ();
232228 assertEquals (lines .get (i ), line , "next() line " + i );
233229 }
234230 assertEquals (false , iterator .hasNext (), "No more expected" );
235- } finally {
236- LineIterator .closeQuietly (iterator );
237231 }
238232 }
239233
@@ -260,8 +254,9 @@ public void testCloseEarly() throws Exception {
260254 final File testFile = new File (temporaryFolder , "LineIterator-closeEarly.txt" );
261255 createLinesFile (testFile , encoding , 3 );
262256
263- final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
264- try {
257+ try (
258+ final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
259+ ) {
265260 // get
266261 assertNotNull ("Line expected" , iterator .next ());
267262 assertTrue (iterator .hasNext (), "More expected" );
@@ -296,8 +291,6 @@ public void testCloseEarly() throws Exception {
296291 } catch (final NoSuchElementException ex ) {
297292 // expected
298293 }
299- } finally {
300- LineIterator .closeQuietly (iterator );
301294 }
302295 }
303296
@@ -315,8 +308,9 @@ private void doTestFileWithSpecifiedLines(final int lineCount) throws IOExceptio
315308 final File testFile = new File (temporaryFolder , fileName );
316309 final List <String > lines = createLinesFile (testFile , encoding , lineCount );
317310
318- final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
319- try {
311+ try (
312+ final LineIterator iterator = FileUtils .lineIterator (testFile , encoding );
313+ ){
320314 try {
321315 iterator .remove ();
322316 fail ("Remove is unsupported" );
@@ -346,8 +340,6 @@ private void doTestFileWithSpecifiedLines(final int lineCount) throws IOExceptio
346340 } catch (final NoSuchElementException expected ) {
347341 // ignore, expected result
348342 }
349- } finally {
350- LineIterator .closeQuietly (iterator );
351343 }
352344 }
353345
0 commit comments