본문 바로가기

emotional developer/detect-Java

Java 7 : The new try-with-resources statement

http://www.baptiste-wicht.com/2010/08/java-7-try-with-resources-statement/

정말 매력적인 기능!! java7 쓸만 하겠다,

private static void customBufferStreamCopy(File source, File target) {
    try (InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target)){
 
        byte[] buf = new byte[8192];
 
        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
반응형