ITEM 9: try-with-resources
static String firstLineOfFile(String path) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(path));
try{
return br.readLine();
}finally {
br.close();
}
} static String firstLineOfFile(String path) throws IOException{
// try - with - resources
try(BufferedReader br = new BufferedReader(new FileReader(path)){
return br.readLine();
}
} static void copy(String src, String dst) throws IOException{
try(InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst))
{
byte[] buf = new byte[BUFFER_SIZE];
int n;
while((n = in.read(buf))>= 0)
out.write(buf, 0, n);
}
}try - with -resources
참고
PreviousITEM 8: Avoid finalizer and cleanerNextITEM 10: The gerneral contract when overriding equlas
Last updated