@@ -432,8 +432,7 @@ public static String byteCountToDisplaySize(final long size) {
432432 */
433433 public static void touch (final File file ) throws IOException {
434434 if (!file .exists ()) {
435- final OutputStream out = openOutputStream (file );
436- IOUtils .closeQuietly (out );
435+ openOutputStream (file ).close ();
437436 }
438437 final boolean success = file .setLastModified (System .currentTimeMillis ());
439438 if (!success ) {
@@ -745,16 +744,9 @@ public static boolean contentEquals(final File file1, final File file2) throws I
745744 return true ;
746745 }
747746
748- InputStream input1 = null ;
749- InputStream input2 = null ;
750- try {
751- input1 = new FileInputStream (file1 );
752- input2 = new FileInputStream (file2 );
747+ try (InputStream input1 = new FileInputStream (file1 );
748+ InputStream input2 = new FileInputStream (file2 )) {
753749 return IOUtils .contentEquals (input1 , input2 );
754-
755- } finally {
756- IOUtils .closeQuietly (input1 );
757- IOUtils .closeQuietly (input2 );
758750 }
759751 }
760752
@@ -798,22 +790,13 @@ public static boolean contentEqualsIgnoreEOL(final File file1, final File file2,
798790 return true ;
799791 }
800792
801- Reader input1 = null ;
802- Reader input2 = null ;
803- try {
804- if (charsetName == null ) {
805- // N.B. make explicit the use of the default charset
806- input1 = new InputStreamReader (new FileInputStream (file1 ), Charset .defaultCharset ());
807- input2 = new InputStreamReader (new FileInputStream (file2 ), Charset .defaultCharset ());
808- } else {
809- input1 = new InputStreamReader (new FileInputStream (file1 ), charsetName );
810- input2 = new InputStreamReader (new FileInputStream (file2 ), charsetName );
811- }
793+ try (Reader input1 = charsetName == null
794+ ? new InputStreamReader (new FileInputStream (file1 ), Charset .defaultCharset ())
795+ : new InputStreamReader (new FileInputStream (file1 ), charsetName );
796+ Reader input2 = charsetName == null
797+ ? new InputStreamReader (new FileInputStream (file2 ), Charset .defaultCharset ())
798+ : new InputStreamReader (new FileInputStream (file2 ), charsetName )) {
812799 return IOUtils .contentEqualsIgnoreEOL (input1 , input2 );
813-
814- } finally {
815- IOUtils .closeQuietly (input1 );
816- IOUtils .closeQuietly (input2 );
817800 }
818801 }
819802
@@ -1133,15 +1116,10 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11331116 throw new IOException ("Destination '" + destFile + "' exists but is a directory" );
11341117 }
11351118
1136- FileInputStream fis = null ;
1137- FileOutputStream fos = null ;
1138- FileChannel input = null ;
1139- FileChannel output = null ;
1140- try {
1141- fis = new FileInputStream (srcFile );
1142- fos = new FileOutputStream (destFile );
1143- input = fis .getChannel ();
1144- output = fos .getChannel ();
1119+ try (FileInputStream fis = new FileInputStream (srcFile );
1120+ FileChannel input = fis .getChannel ();
1121+ FileOutputStream fos = new FileOutputStream (destFile );
1122+ FileChannel output = fos .getChannel ()) {
11451123 final long size = input .size (); // TODO See IO-386
11461124 long pos = 0 ;
11471125 long count = 0 ;
@@ -1154,20 +1132,6 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11541132 }
11551133 pos += bytesCopied ;
11561134 }
1157-
1158- output .close ();
1159- output = null ;
1160-
1161- fos .close ();
1162- fos = null ;
1163-
1164- input .close ();
1165- input = null ;
1166-
1167- fis .close ();
1168- fis = null ;
1169- } finally {
1170- IOUtils .closeQuietly (output , fos , input , fis );
11711135 }
11721136
11731137 final long srcLen = srcFile .length (); // TODO See IO-386
@@ -1536,10 +1500,8 @@ public static void copyURLToFile(final URL source, final File destination,
15361500 * @since 2.0
15371501 */
15381502 public static void copyInputStreamToFile (final InputStream source , final File destination ) throws IOException {
1539- try {
1540- copyToFile (source , destination );
1541- } finally {
1542- IOUtils .closeQuietly (source );
1503+ try (InputStream in = source ) {
1504+ copyToFile (in , destination );
15431505 }
15441506 }
15451507
@@ -1561,12 +1523,9 @@ public static void copyInputStreamToFile(final InputStream source, final File de
15611523 * @since 2.5
15621524 */
15631525 public static void copyToFile (final InputStream source , final File destination ) throws IOException {
1564- final FileOutputStream output = openOutputStream (destination );
1565- try {
1566- IOUtils .copy (source , output );
1567- output .close (); // don't swallow close Exception if copy completes normally
1568- } finally {
1569- IOUtils .closeQuietly (output );
1526+ try (InputStream in = source ;
1527+ OutputStream out = openOutputStream (destination )) {
1528+ IOUtils .copy (in , out );
15701529 }
15711530 }
15721531
@@ -1772,12 +1731,8 @@ public static boolean waitFor(final File file, final int seconds) {
17721731 * @since 2.3
17731732 */
17741733 public static String readFileToString (final File file , final Charset encoding ) throws IOException {
1775- InputStream in = null ;
1776- try {
1777- in = openInputStream (file );
1734+ try (InputStream in = openInputStream (file )) {
17781735 return IOUtils .toString (in , Charsets .toCharset (encoding ));
1779- } finally {
1780- IOUtils .closeQuietly (in );
17811736 }
17821737 }
17831738
@@ -1822,12 +1777,8 @@ public static String readFileToString(final File file) throws IOException {
18221777 * @since 1.1
18231778 */
18241779 public static byte [] readFileToByteArray (final File file ) throws IOException {
1825- InputStream in = null ;
1826- try {
1827- in = openInputStream (file );
1780+ try (InputStream in = openInputStream (file )) {
18281781 return IOUtils .toByteArray (in ); // Do NOT use file.length() - see IO-453
1829- } finally {
1830- IOUtils .closeQuietly (in );
18311782 }
18321783 }
18331784
@@ -1842,12 +1793,8 @@ public static byte[] readFileToByteArray(final File file) throws IOException {
18421793 * @since 2.3
18431794 */
18441795 public static List <String > readLines (final File file , final Charset encoding ) throws IOException {
1845- InputStream in = null ;
1846- try {
1847- in = openInputStream (file );
1796+ try (InputStream in = openInputStream (file )) {
18481797 return IOUtils .readLines (in , Charsets .toCharset (encoding ));
1849- } finally {
1850- IOUtils .closeQuietly (in );
18511798 }
18521799 }
18531800
@@ -1917,11 +1864,15 @@ public static LineIterator lineIterator(final File file, final String encoding)
19171864 try {
19181865 in = openInputStream (file );
19191866 return IOUtils .lineIterator (in , encoding );
1920- } catch (final IOException ex ) {
1921- IOUtils .closeQuietly (in );
1922- throw ex ;
1923- } catch (final RuntimeException ex ) {
1924- IOUtils .closeQuietly (in );
1867+ } catch (final IOException | RuntimeException ex ) {
1868+ try {
1869+ if (in != null ) {
1870+ in .close ();
1871+ }
1872+ }
1873+ catch (final IOException e ) {
1874+ ex .addSuppressed (e );
1875+ }
19251876 throw ex ;
19261877 }
19271878 }
@@ -1986,15 +1937,10 @@ public static void writeStringToFile(final File file, final String data, final S
19861937 * @throws IOException in case of an I/O error
19871938 * @since 2.3
19881939 */
1989- public static void writeStringToFile (final File file , final String data , final Charset encoding , final boolean
1990- append ) throws IOException {
1991- OutputStream out = null ;
1992- try {
1993- out = openOutputStream (file , append );
1940+ public static void writeStringToFile (final File file , final String data , final Charset encoding ,
1941+ final boolean append ) throws IOException {
1942+ try (OutputStream out = openOutputStream (file , append )) {
19941943 IOUtils .write (data , out , encoding );
1995- out .close (); // don't swallow close Exception if copy completes normally
1996- } finally {
1997- IOUtils .closeQuietly (out );
19981944 }
19991945 }
20001946
@@ -2200,13 +2146,8 @@ public static void writeByteArrayToFile(final File file, final byte[] data, fina
22002146 */
22012147 public static void writeByteArrayToFile (final File file , final byte [] data , final int off , final int len ,
22022148 final boolean append ) throws IOException {
2203- OutputStream out = null ;
2204- try {
2205- out = openOutputStream (file , append );
2149+ try (OutputStream out = openOutputStream (file , append )) {
22062150 out .write (data , off , len );
2207- out .close (); // don't swallow close Exception if copy completes normally
2208- } finally {
2209- IOUtils .closeQuietly (out );
22102151 }
22112152 }
22122153
@@ -2317,15 +2258,8 @@ public static void writeLines(final File file, final String encoding, final Coll
23172258 */
23182259 public static void writeLines (final File file , final String encoding , final Collection <?> lines ,
23192260 final String lineEnding , final boolean append ) throws IOException {
2320- FileOutputStream out = null ;
2321- try {
2322- out = openOutputStream (file , append );
2323- final BufferedOutputStream buffer = new BufferedOutputStream (out );
2324- IOUtils .writeLines (lines , lineEnding , buffer , encoding );
2325- buffer .flush ();
2326- out .close (); // don't swallow close Exception if copy completes normally
2327- } finally {
2328- IOUtils .closeQuietly (out );
2261+ try (OutputStream out = new BufferedOutputStream (openOutputStream (file , append ))) {
2262+ IOUtils .writeLines (lines , lineEnding , out , encoding );
23292263 }
23302264 }
23312265
@@ -2880,12 +2814,8 @@ public static Checksum checksum(final File file, final Checksum checksum) throws
28802814 if (file .isDirectory ()) {
28812815 throw new IllegalArgumentException ("Checksums can't be computed on directories" );
28822816 }
2883- InputStream in = null ;
2884- try {
2885- in = new CheckedInputStream (new FileInputStream (file ), checksum );
2817+ try (InputStream in = new CheckedInputStream (new FileInputStream (file ), checksum )) {
28862818 IOUtils .copy (in , new NullOutputStream ());
2887- } finally {
2888- IOUtils .closeQuietly (in );
28892819 }
28902820 return checksum ;
28912821 }
0 commit comments