jdk 1.7之前

public static void test1(){
        FileInputStream ins = null;
        FileOutputStream out = null;
        try {
            ins = new FileInputStream(new File("G://aa.text"));
            out = new FileOutputStream(new File("G://bb.text"));
            //业务逻辑
        }catch (FileNotFoundException ex){
            ex.printStackTrace();
        }finally {
            //关闭资源
            if(ins != null){
                try {
                    ins.close();
                }catch (Exception insex){
                    insex.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                }catch (Exception outex){
                    outex.printStackTrace();
                }
            }
        }
    }

jdk 1.7及之后

public static void test2(){
        try(FileInputStream ins = new FileInputStream(new File("G:/aa.text"));
            FileOutputStream out = new FileOutputStream(new File("G://bb.text"))){
            //业务逻辑
        }catch (FileNotFoundException fnex){
            fnex.printStackTrace();
        }catch (IOException ioex){
            ioex.printStackTrace();
        }
    }

资源一般是指:实现了Closeable接口或者AutoCloseable接口,这种资源使用完毕后都需要关闭。而且实现这两个接口的都可以用上述方法关闭资源

注意:上述关闭方式android版本19及以上

jdk 1.7 优雅的关闭资源_android

jdk 1.7 优雅的关闭资源_业务逻辑_02