Close a stream in a try/catch blockTag(s): IO
You close them in the finally clause using a special method.
public static boolean compareFile(File file1, File file2) {
BufferedInputStream in1 = null;
BufferedInputStream in2 = null;
try {
in1 = new BufferedInputStream(new FileInputStream(file1));
in2 = new BufferedInputStream(new FileInputStream(file2));
boolean result = true;
int i;
do {
i = in1.read();
result = (i == in2.read());
} while ( result && i != -1 );
return result;
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
close(in1);
close(in2);
}
return false;
}
private static void close( InputStream is ) {
if ( is != null ) {
try {
is.close();
}
catch ( IOException ioe ) {
ioe.printStackTrace();
}
}
}
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com