@@ -108,13 +108,22 @@ public static long convertSizeToLong(String aString, long defaultValue) {
108
108
*/
109
109
public static byte [] readContent (File file ) {
110
110
byte [] buffer = new byte [(int ) file .length ()];
111
+ BufferedInputStream is = null ;
111
112
try {
112
- BufferedInputStream is = new BufferedInputStream (new FileInputStream (file ));
113
+ is = new BufferedInputStream (new FileInputStream (file ));
113
114
is .read (buffer , 0 , buffer .length );
114
- is .close ();
115
115
} catch (Throwable t ) {
116
116
System .err .println ("Failed to read byte content of " + file .getAbsolutePath ());
117
117
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
+ }
118
127
}
119
128
return buffer ;
120
129
}
@@ -128,9 +137,9 @@ public static long convertSizeToLong(String aString, long defaultValue) {
128
137
*/
129
138
public static String readContent (File file , String lineEnding ) {
130
139
StringBuilder sb = new StringBuilder ();
140
+ InputStreamReader is = null ;
131
141
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" ));
134
143
BufferedReader reader = new BufferedReader (is );
135
144
String line = null ;
136
145
while ((line = reader .readLine ()) != null ) {
@@ -139,10 +148,18 @@ public static String readContent(File file, String lineEnding) {
139
148
sb .append (lineEnding );
140
149
}
141
150
}
142
- reader .close ();
143
151
} catch (Throwable t ) {
144
152
System .err .println ("Failed to read content of " + file .getAbsolutePath ());
145
153
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
+ }
146
163
}
147
164
return sb .toString ();
148
165
}
@@ -154,15 +171,24 @@ public static String readContent(File file, String lineEnding) {
154
171
* @param content
155
172
*/
156
173
public static void writeContent (File file , String content ) {
174
+ OutputStreamWriter os = null ;
157
175
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" ));
160
177
BufferedWriter writer = new BufferedWriter (os );
161
178
writer .append (content );
162
- writer .close ();
179
+ writer .flush ();
163
180
} catch (Throwable t ) {
164
181
System .err .println ("Failed to write content of " + file .getAbsolutePath ());
165
182
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
+ }
166
192
}
167
193
}
168
194
@@ -219,11 +245,11 @@ public static void copy(File destinationFolder, File... filesOrFolders)
219
245
}
220
246
} finally {
221
247
try {
222
- bufin .close ();
248
+ if ( bufin != null ) bufin .close ();
223
249
} catch (Throwable t ) {
224
250
}
225
251
try {
226
- fos .close ();
252
+ if ( fos != null ) fos .close ();
227
253
} catch (Throwable t ) {
228
254
}
229
255
}
0 commit comments