Java try/catch/finally best practices while acquiring/closing resources

后端 未结 8 1057
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 13:43

While working on a school project, I wrote the following code:

FileOutputStream fos;
ObjectOutputStream oos;
try {
    fos = new FileOutputStream(file);
             


        
相关标签:
8条回答
  • 2020-11-27 14:41

    Unfortunately there is no language level support. But there are lot of libraries out there that makes this simple. Check commons-io library. Or modern google-guava @ http://guava-libraries.googlecode.com/svn/trunk/javadoc/index.html

    0 讨论(0)
  • 2020-11-27 14:42

    You're doing it right. It bothers the crap out of me too. You should initialize those streams to null explicitly - it's common convention. All you can do is join the club and want for using.

    0 讨论(0)
提交回复
热议问题