最近发现try-with-resource挺好用的,特此记录1下:
正规青年:
try {
FileInputStream in = new FileInputStream(inputFileName);
FileOutputStream out = new FileOutputStream(outputFileName)
//in out do samething
} catch (IOException x) {
//System.err.format("IOException: ");
} finally {
if (in != null) in .close();
if (out != null) out .close();
}
牛b青年;
try (FileInputStream in = new FileInputStream(inputFileName);
FileOutputStream out = new FileOutputStream(outputFileName)) {
//in out do samething
}catch(){
}
try就会自动关闭resource,防止内存泄露。