@@ -94,47 +94,35 @@ public void setUp() throws Exception {
9494 throw new IOException ("Cannot create file " + testFile1
9595 + " as the parent directory does not exist" );
9696 }
97- final BufferedOutputStream output3 =
98- new BufferedOutputStream (new FileOutputStream (testFile1 ));
99- try {
97+ try (final BufferedOutputStream output3 =
98+ new BufferedOutputStream (new FileOutputStream (testFile1 ))) {
10099 TestUtils .generateTestData (output3 , (long ) testFile1Size );
101- } finally {
102- IOUtils .closeQuietly (output3 );
103100 }
104101 if (!testFile2 .getParentFile ().exists ()) {
105102 throw new IOException ("Cannot create file " + testFile2
106103 + " as the parent directory does not exist" );
107104 }
108- final BufferedOutputStream output2 =
109- new BufferedOutputStream (new FileOutputStream (testFile2 ));
110- try {
105+ try (final BufferedOutputStream output2 =
106+ new BufferedOutputStream (new FileOutputStream (testFile2 ))) {
111107 TestUtils .generateTestData (output2 , (long ) testFile2Size );
112- } finally {
113- IOUtils .closeQuietly (output2 );
114108 }
115109 FileUtils .deleteDirectory (getTestDirectory ());
116110 getTestDirectory ().mkdirs ();
117111 if (!testFile1 .getParentFile ().exists ()) {
118112 throw new IOException ("Cannot create file " + testFile1
119113 + " as the parent directory does not exist" );
120114 }
121- final BufferedOutputStream output1 =
122- new BufferedOutputStream (new FileOutputStream (testFile1 ));
123- try {
115+ try (final BufferedOutputStream output1 =
116+ new BufferedOutputStream (new FileOutputStream (testFile1 ))) {
124117 TestUtils .generateTestData (output1 , (long ) testFile1Size );
125- } finally {
126- IOUtils .closeQuietly (output1 );
127118 }
128119 if (!testFile2 .getParentFile ().exists ()) {
129120 throw new IOException ("Cannot create file " + testFile2
130121 + " as the parent directory does not exist" );
131122 }
132- final BufferedOutputStream output =
133- new BufferedOutputStream (new FileOutputStream (testFile2 ));
134- try {
123+ try (final BufferedOutputStream output =
124+ new BufferedOutputStream (new FileOutputStream (testFile2 ))) {
135125 TestUtils .generateTestData (output , (long ) testFile2Size );
136- } finally {
137- IOUtils .closeQuietly (output );
138126 }
139127 }
140128
@@ -216,41 +204,29 @@ public void testGetUserDirectory() {
216204 public void test_openInputStream_exists () throws Exception {
217205 final File file = new File (getTestDirectory (), "test.txt" );
218206 TestUtils .createLineBasedFile (file , new String []{"Hello" });
219- FileInputStream in = null ;
220- try {
221- in = FileUtils .openInputStream (file );
207+ try (FileInputStream in = FileUtils .openInputStream (file )) {
222208 assertEquals ('H' , in .read ());
223- } finally {
224- IOUtils .closeQuietly (in );
225209 }
226210 }
227211
228212 @ Test
229213 public void test_openInputStream_existsButIsDirectory () throws Exception {
230214 final File directory = new File (getTestDirectory (), "subdir" );
231215 directory .mkdirs ();
232- FileInputStream in = null ;
233- try {
234- in = FileUtils .openInputStream (directory );
216+ try (FileInputStream in = FileUtils .openInputStream (directory )) {
235217 fail ();
236218 } catch (final IOException ioe ) {
237219 // expected
238- } finally {
239- IOUtils .closeQuietly (in );
240220 }
241221 }
242222
243223 @ Test
244224 public void test_openInputStream_notExists () throws Exception {
245225 final File directory = new File (getTestDirectory (), "test.txt" );
246- FileInputStream in = null ;
247- try {
248- in = FileUtils .openInputStream (directory );
226+ try (FileInputStream in = FileUtils .openInputStream (directory )) {
249227 fail ();
250228 } catch (final IOException ioe ) {
251229 // expected
252- } finally {
253- IOUtils .closeQuietly (in );
254230 }
255231 }
256232
@@ -262,12 +238,8 @@ void openOutputStream_noParent(final boolean createFile) throws Exception {
262238 if (createFile ) {
263239 TestUtils .createLineBasedFile (file , new String []{"Hello" });
264240 }
265- FileOutputStream out = null ;
266- try {
267- out = FileUtils .openOutputStream (file );
241+ try (FileOutputStream out = FileUtils .openOutputStream (file )) {
268242 out .write (0 );
269- } finally {
270- IOUtils .closeQuietly (out );
271243 }
272244 assertTrue (file .exists ());
273245 } finally {
@@ -287,17 +259,12 @@ public void test_openOutputStream_noParentNoFile() throws Exception {
287259 openOutputStream_noParent (false );
288260 }
289261
290-
291262 @ Test
292263 public void test_openOutputStream_exists () throws Exception {
293264 final File file = new File (getTestDirectory (), "test.txt" );
294265 TestUtils .createLineBasedFile (file , new String []{"Hello" });
295- FileOutputStream out = null ;
296- try {
297- out = FileUtils .openOutputStream (file );
266+ try (FileOutputStream out = FileUtils .openOutputStream (file )) {
298267 out .write (0 );
299- } finally {
300- IOUtils .closeQuietly (out );
301268 }
302269 assertTrue (file .exists ());
303270 }
@@ -306,26 +273,18 @@ public void test_openOutputStream_exists() throws Exception {
306273 public void test_openOutputStream_existsButIsDirectory () throws Exception {
307274 final File directory = new File (getTestDirectory (), "subdir" );
308275 directory .mkdirs ();
309- FileOutputStream out = null ;
310- try {
311- out = FileUtils .openOutputStream (directory );
276+ try (FileOutputStream out = FileUtils .openOutputStream (directory )) {
312277 fail ();
313278 } catch (final IOException ioe ) {
314279 // expected
315- } finally {
316- IOUtils .closeQuietly (out );
317280 }
318281 }
319282
320283 @ Test
321284 public void test_openOutputStream_notExists () throws Exception {
322285 final File file = new File (getTestDirectory (), "a/test.txt" );
323- FileOutputStream out = null ;
324- try {
325- out = FileUtils .openOutputStream (file );
286+ try (FileOutputStream out = FileUtils .openOutputStream (file )) {
326287 out .write (0 );
327- } finally {
328- IOUtils .closeQuietly (out );
329288 }
330289 assertTrue (file .exists ());
331290 }
@@ -341,14 +300,10 @@ public void test_openOutputStream_notExistsCannotCreate() throws Exception {
341300 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" +
342301 "abcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyzabcdevwxyz" ; // 300 chars
343302 final File file = new File (getTestDirectory (), "a/" + longStr + "/test.txt" );
344- FileOutputStream out = null ;
345- try {
346- out = FileUtils .openOutputStream (file );
303+ try (FileOutputStream out = FileUtils .openOutputStream (file )) {
347304 fail ();
348305 } catch (final IOException ioe ) {
349306 // expected
350- } finally {
351- IOUtils .closeQuietly (out );
352307 }
353308 }
354309
0 commit comments