@@ -108,13 +108,22 @@ public static long convertSizeToLong(String aString, long defaultValue) {
108108 */
109109 public static byte [] readContent (File file ) {
110110 byte [] buffer = new byte [(int ) file .length ()];
111+ BufferedInputStream is = null ;
111112 try {
112- BufferedInputStream is = new BufferedInputStream (new FileInputStream (file ));
113+ is = new BufferedInputStream (new FileInputStream (file ));
113114 is .read (buffer , 0 , buffer .length );
114- is .close ();
115115 } catch (Throwable t ) {
116116 System .err .println ("Failed to read byte content of " + file .getAbsolutePath ());
117117 t .printStackTrace ();
118+ } finally {
119+ if (is != null ) {
120+ try {
121+ is .close ();
122+ } catch (IOException ioe ) {
123+ System .err .println ("Failed to close file " + file .getAbsolutePath ());
124+ ioe .printStackTrace ();
125+ }
126+ }
118127 }
119128 return buffer ;
120129 }
@@ -128,9 +137,9 @@ public static long convertSizeToLong(String aString, long defaultValue) {
128137 */
129138 public static String readContent (File file , String lineEnding ) {
130139 StringBuilder sb = new StringBuilder ();
140+ InputStreamReader is = null ;
131141 try {
132- InputStreamReader is = new InputStreamReader (new FileInputStream (file ),
133- Charset .forName ("UTF-8" ));
142+ is = new InputStreamReader (new FileInputStream (file ), Charset .forName ("UTF-8" ));
134143 BufferedReader reader = new BufferedReader (is );
135144 String line = null ;
136145 while ((line = reader .readLine ()) != null ) {
@@ -139,10 +148,18 @@ public static String readContent(File file, String lineEnding) {
139148 sb .append (lineEnding );
140149 }
141150 }
142- reader .close ();
143151 } catch (Throwable t ) {
144152 System .err .println ("Failed to read content of " + file .getAbsolutePath ());
145153 t .printStackTrace ();
154+ } finally {
155+ if (is != null ) {
156+ try {
157+ is .close ();
158+ } catch (IOException ioe ) {
159+ System .err .println ("Failed to close file " + file .getAbsolutePath ());
160+ ioe .printStackTrace ();
161+ }
162+ }
146163 }
147164 return sb .toString ();
148165 }
@@ -154,15 +171,24 @@ public static String readContent(File file, String lineEnding) {
154171 * @param content
155172 */
156173 public static void writeContent (File file , String content ) {
174+ OutputStreamWriter os = null ;
157175 try {
158- OutputStreamWriter os = new OutputStreamWriter (new FileOutputStream (file ),
159- Charset .forName ("UTF-8" ));
176+ os = new OutputStreamWriter (new FileOutputStream (file ), Charset .forName ("UTF-8" ));
160177 BufferedWriter writer = new BufferedWriter (os );
161178 writer .append (content );
162- writer .close ();
179+ writer .flush ();
163180 } catch (Throwable t ) {
164181 System .err .println ("Failed to write content of " + file .getAbsolutePath ());
165182 t .printStackTrace ();
183+ } finally {
184+ if (os != null ) {
185+ try {
186+ os .close ();
187+ } catch (IOException ioe ) {
188+ System .err .println ("Failed to close file " + file .getAbsolutePath ());
189+ ioe .printStackTrace ();
190+ }
191+ }
166192 }
167193 }
168194
@@ -219,11 +245,11 @@ public static void copy(File destinationFolder, File... filesOrFolders)
219245 }
220246 } finally {
221247 try {
222- bufin .close ();
248+ if ( bufin != null ) bufin .close ();
223249 } catch (Throwable t ) {
224250 }
225251 try {
226- fos .close ();
252+ if ( fos != null ) fos .close ();
227253 } catch (Throwable t ) {
228254 }
229255 }
0 commit comments