我是否必须关闭由PrintStream包装的FileOutputStream? [英] Do I have to close FileOutputStream which is wrapped by PrintStream?

查看:156
本文介绍了我是否必须关闭由PrintStream包装的FileOutputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileOutputStream和PrintStream,如下所示:

  class PrintStreamDemo {
public static void main(String args []){
FileOutputStream out;
PrintStream ps; //声明一个打印流对象
try {
//创建一个新的文件输出流
out = new FileOutputStream(myfile.txt);

//将打印流连接到输出流
ps = new PrintStream(out);

ps.println(此数据写入文件:);
System.err.println(写成功);
ps.close();
}
catch(例外e){
System.err.println(写入文件时出错);
}
}
}

我只关闭PrintStream。
我是否需要关闭FileOutputStream( out.close(); )?

解决方案

不,你只需要关闭最外面的流。它将一直委托给包装的流。



然而,你的代码包含一个概念上的失败,关闭应该发生在 finally ,否则当代码在开始和结束之间抛出异常时它永远不会关闭。



例如

  public static void main(String args [])抛出IOException {
PrintStream ps = null;

try {
ps = new PrintStream(new FileOutputStream(myfile.txt));
ps.println(此数据写入文件:);
System.out.println(写成功);
} catch(IOException e){
System.err.println(写入文件时出错);
扔e;
} finally {
if(ps!= null)ps.close();
}
}

(请注意,我将代码更改为抛出异常,以便您了解问题的原因,异常即包含有关问题原因的详细信息)



<或者,当您已经使用Java 7时,您还可以使用ARM(自动资源管理;也称为尝试使用资源),这样您就不需要自己关闭任何内容:

  public static void main(String args [])throws IOException {
try(PrintStream ps = new PrintStream(new FileOutputStream(myfile.txt))){
ps.println(This数据被写入文件:);
System.out.println(写成功);
} catch(IOException e){
System.err.println(写入文件时出错);
扔e;
}
}


I'm using FileOutputStream with PrintStream like this:

class PrintStreamDemo {  
  public static void main(String args[]){ 
   FileOutputStream out; 
   PrintStream ps; // declare a print stream object
   try {
     // Create a new file output stream
     out = new FileOutputStream("myfile.txt");

     // Connect print stream to the output stream
     ps = new PrintStream(out);

     ps.println ("This data is written to a file:");
     System.err.println ("Write successfully");
     ps.close();
   }
   catch (Exception e){
     System.err.println ("Error in writing to file");
   }
  }
}

Im closing only the PrintStream. Do i need to close allso the FileOutputStream (out.close();)?

解决方案

No, you only need to close the outermost stream. It will delegate all the way to the wrapped streams.

However, your code contains one conceptual failure, the close should happen in finally, otherwise it's never closed when the code throws an exception between opening and closing.

E.g.

public static void main(String args[]) throws IOException { 
    PrintStream ps = null;

    try {
        ps = new PrintStream(new FileOutputStream("myfile.txt"));
        ps.println("This data is written to a file:");
        System.out.println("Write successfully");
    } catch (IOException e) {
        System.err.println("Error in writing to file");
        throw e;
    } finally {
        if (ps != null) ps.close();
    }
}

(note that I changed the code to throw the exception so that you understand the reason of the problem, the exception namely contains detailed information about the cause of the problem)

Or, when you're already on Java 7, then you can also make use of ARM (Automatic Resource Management; also known as try-with-resources) so that you don't need to close anything yourself:

public static void main(String args[]) throws IOException { 
    try (PrintStream ps = new PrintStream(new FileOutputStream("myfile.txt"))) {
        ps.println("This data is written to a file:");
        System.out.println("Write successfully");
    } catch (IOException e) {
        System.err.println("Error in writing to file");
        throw e;
    }
}

这篇关于我是否必须关闭由PrintStream包装的FileOutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆