Skip to content

Commit da49a1f

Browse files
author
Stephen Colebourne
committed
Increase certainty that files are closed in case of error
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@366357 13f79535-47bb-0310-9956-ffa450edef68
1 parent fe6e1d9 commit da49a1f

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.net.URL;
2828
import java.util.Collection;
2929
import java.util.Date;
30-
import java.util.List;
3130
import java.util.Iterator;
31+
import java.util.List;
3232

3333
import org.apache.commons.io.filefilter.DirectoryFileFilter;
3434
import org.apache.commons.io.filefilter.FalseFileFilter;
@@ -815,6 +815,7 @@ public static boolean waitFor(File file, int seconds) {
815815
//-----------------------------------------------------------------------
816816
/**
817817
* Reads the contents of a file into a String.
818+
* The file is always closed.
818819
* <p>
819820
* There is no readFileToString method without encoding parameter because
820821
* the default encoding can differ between platforms and therefore results
@@ -828,8 +829,9 @@ public static boolean waitFor(File file, int seconds) {
828829
*/
829830
public static String readFileToString(
830831
File file, String encoding) throws IOException {
831-
InputStream in = new FileInputStream(file);
832+
InputStream in = null;
832833
try {
834+
in = new FileInputStream(file);
833835
return IOUtils.toString(in, encoding);
834836
} finally {
835837
IOUtils.closeQuietly(in);
@@ -838,15 +840,17 @@ public static String readFileToString(
838840

839841
/**
840842
* Reads the contents of a file into a byte array.
843+
* The file is always closed.
841844
*
842845
* @param file the file to read
843846
* @return the file contents or null if read failed
844847
* @throws IOException in case of an I/O error
845848
* @since Commons IO 1.1
846849
*/
847850
public static byte[] readFileToByteArray(File file) throws IOException {
848-
InputStream in = new FileInputStream(file);
851+
InputStream in = null;
849852
try {
853+
in = new FileInputStream(file);
850854
return IOUtils.toByteArray(in);
851855
} finally {
852856
IOUtils.closeQuietly(in);
@@ -855,6 +859,7 @@ public static byte[] readFileToByteArray(File file) throws IOException {
855859

856860
/**
857861
* Reads the contents of a file line by line to a List of Strings.
862+
* The file is always closed.
858863
* <p>
859864
* There is no readLines method without encoding parameter because
860865
* the default encoding can differ between platforms and therefore results
@@ -868,8 +873,9 @@ public static byte[] readFileToByteArray(File file) throws IOException {
868873
* @since Commons IO 1.1
869874
*/
870875
public static List readLines(File file, String encoding) throws IOException {
871-
InputStream in = new FileInputStream(file);
876+
InputStream in = null;
872877
try {
878+
in = new FileInputStream(file);
873879
return IOUtils.readLines(in, encoding);
874880
} finally {
875881
IOUtils.closeQuietly(in);

0 commit comments

Comments
 (0)