Java开发小技巧(二)之try-with-resource的使用

        下面这两段代码我想写过Java的人都很清楚,也许自己就经常这么写。

//复制文件
FileInputStream fis = null;
FileOutputStream fos = null;
try {
    fis = new FileInputStream("D:\\readme.txt");
    fos = new FileOutputStream("D:\\test.txt");
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (fos != null)
            fos.close();
        if (fis != null)
            fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


//连接数据库并查询数据
Connection connection = null;
ResultSet resultSet = null;
try {
    connection = DBHelper.getConnection();
    Statement statement = connection.createStatement();
    resultSet = statement.executeQuery("select * from table1");
    while (resultSet.next()) {
        System.out.println(resultSet.getString(1));
        System.out.println(resultSet.getString(2));
        System.out.println(resultSet.getString(3));
    }
} catch (SQLException throwables) {
    throwables.printStackTrace();
} finally {
    try {
        if (resultSet != null)
            resultSet.close();
        if (connection != null)
            connection.close();
        ;
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}

        尽管我们对这种模块化的代码深恶痛绝,明明有用的代码就那么行句,为了执行这几行代码我们又不得不写多几倍的代码,人在讲话身不由己啊。

        好在Java社区的开发者也遇到了这种痛,在jdk1.7之后终于给出了一个好的解决方案,那就是try-with-resource的写法,上面两段代码在使用try-with-resource后可以写成这个样子

try(FileInputStream fis = new FileInputStream("D:\\readme.txt");
    FileOutputStream fos = new FileOutputStream("D:\\test.txt");) {
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
try(Connection connection = DBHelper.getConnection();
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from table1");) {
    while (resultSet.next()) {
        System.out.println(resultSet.getString(1));
        System.out.println(resultSet.getString(2));
        System.out.println(resultSet.getString(3));
    }
} catch (SQLException throwables) {
    throwables.printStackTrace();
}

        咋一看,确实代码优雅了不少,再也不用在finally里面再去try catch了,今天我们就聊聊这个try-with-resource,从字面上来看try-with-resource意思是尝试使用资源,那它的执行流程是什么样的呢?在什么情况下能使用这种方式呢?

        首先什么是资源呢?但从中文理解这个范围就太大了,一个文件、一个数据库连接是不是资源?当时Java里面不是这么理解,在try-with-resource里面,必须要实现了AutoCloseable结构才能算是资源,那为什么一定要实现AutoCloseable接口呢?因为在try后面括号定义的对象,在try里面的代码执行完成或者抛出异常后,会去调用close方法,而这个close方法就是AutoCloseable定义的方法,所以只有实现了AutoCloseable接口的类才能放在try-with-resource里面。后面在开发的过程中可以尝试使用这种方式去关闭资源哦,相信我,用了一次你就会爱上他的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值