集册 Java实例教程 使用try / catch块管理资源

使用try / catch块管理资源

欢马劈雪     最近更新时间:2020-01-02 10:19:05

458
将自动资源管理与try-with-resources语句一起使用。


import java.io.BufferedOutputStream;
/*
n  o  w  j  a  v  a . c o m 提 供
*/

import java.io.DataOutputStream;

import java.io.FileOutputStream;


public class Main {

  public static void main(String[] args) {


    try (FileOutputStream fos = new FileOutputStream("out.log");

        BufferedOutputStream bos = new BufferedOutputStream(fos);

        DataOutputStream dos = new DataOutputStream(bos)) {

      dos.writeUTF("This is being written");

      // If an exception occurs here, resources will be handled correctly/** nowjava.com 提供 **/

展开阅读全文