Should .close() be put in finally block or not?

The following are 3 different ways to close a output writer. The first one puts close() method in try clause, the second one puts close in finally clause, and the third one uses a try-with-resources statement. Which one is the right or the best?

{PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(“out.txt”, true)));out.println(“the text”);out.close();} catch (IOException e) {e.printStackTrace();},昨晚多几分钟的准备,今天少几小时的麻烦。

Should .close() be put in finally block or not?

相关文章:

你感兴趣的文章:

标签云: