zoukankan      html  css  js  c++  java
  • JDK7-try-with-resources

    jdk7之前的写法try-catch-finally

    package com.wjz.jdk7.before;
     
    import java.io.*;
     
    public class TryCatchFinally {
     
        private static final String src = "D:/src.txt";
        private static final String dst = "D:/dst.txt";
     
        public static void main(String[] args) {
            copy(src, dst);
            System.out.println("复制成功!");
        }
     
        public static void copy(String src, String dst) {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new FileInputStream(src);
                out = new FileOutputStream(dst);
                byte[] buff = new byte[1024];
                int n;
                while ((n = in.read(buff)) >= 0) {
                    out.write(buff, 0, n);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    jdk7之后的写法try-with-resources

    package com.wjz.jdk7.after;
    
    import java.io.*;
    
    /**
     * try-with-resources 声明要求其中定义的变量实现 AutoCloseable 接口,
     * 这样系统可以自动调用它们的close方法从而替代了finally中关闭资源的功能,
     * 甜甜的语法糖
     */
    public class TryWithResources {
    
        private static final String src = "D:/src.txt";
        private static final String dst = "D:/dst.txt";
    
        public static void main(String[] args) {
            copy(src, dst);
            System.out.println("复制成功!");
        }
    
        public static void copy(String src, String dst) {
            try (InputStream in = new FileInputStream(src);
                 OutputStream out = new FileOutputStream(dst)) {
                byte[] buff = new byte[1024];
                int n;
                while ((n = in.read(buff)) >= 0) {
                    out.write(buff, 0, n);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    自定义资源

    package com.wjz.jdk7.autoclose;
    
    import java.io.IOException;
    
    public class Resource implements AutoCloseable {
    
        public void sayHello() {
            System.out.println("hello");
        }
    
        @Override
        public void close() throws IOException {
            System.out.println("Resource is closed");
        }
    
        public static void main(String[] args) {
            try (Resource r = new Resource()) {
                r.sayHello();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

      

  • 相关阅读:
    bzoj4325: NOIP2015 斗地主(爆搜+模拟)
    bzoj3631: [JLOI2014]松鼠的新家(LCA+差分)
    bzoj3555: [Ctsc2014]企鹅QQ (Hash)
    bzoj1455: 罗马游戏 + bzoj2809: Dispatching(可并堆)
    bzoj1510: [POI2006]Kra-The Disks(单调栈)
    bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
    bzoj3048+3049+3050
    bzoj3083 遥远的国度 && bzoj3626 LCA (树链剖分)
    bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)
    bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/13155925.html
Copyright © 2011-2022 走看看