Java需要帮助关闭Stream [英] Java need help to close Stream

查看:124
本文介绍了Java需要帮助关闭Stream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DOM解析器中有一个DOM解析器,并且在写入 XML Stream c>文件



我的程序创建一个文件,并调用这个代码(看下面)写入 xml-nodes 到文件。我的程序还包含一个删除文件功能,它不起作用,因为有一些Streams未封闭的Streams,所以删除文件返回false。



当我重新运行程序时,我可以删除该文件..但是每当我创建一个新的,或在程序中的现有文件中编辑该程序似乎留下一些打开的 s。



下面的代码是剩下一个未关闭的部分。我试图以某种方式关闭 Streamresult ,但没有办法关闭它。

  public void rewriteXML(){
try {

DocumentBuilderFactory song = DocumentBuilderFactory.newInstance();
DocumentBuilder songBuilder = song.newDocumentBuilder();

文档doc = songBuilder.newDocument();
元素playlist = doc.createElement(playlist);
doc.appendChild(播放列表);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer tr = tf.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT,yes);
DOMSource dom = new DOMSource(doc);
StreamResult sr = new StreamResult(new FileOutputStream(fileName,false));

tr.transform(dom,sr);

System.out.println(done);

} catch(ParserConfigurationException pce){
pce.printStackTrace();
} catch(TransformerException fce){
fce.printStackTrace();
} catch(IOException ie){
ie.printStackTrace();
}
}

我需要一些方法来关闭 Stream ,或者是为了打开 Stream 的错误修复方法,因为我不太确定什么 Stream 未关闭。

解决方案

FileOutputStream 一个 StreamResult 不是一个 Stream ,但它包含一个。诀窍是找到它,然后关闭它。



sr.getOutputStream()。close() p>

Michael Kay(评论)指出, StreamResult 留下任何 Stream 传递给它打开。如果您不知道这个并且想要安全,那么值得注意的是,如果您尝试关闭它们两次,Java中的大部分流都不会抛出异常。如果您不确定,请再次关闭。


I got a DOM parser in my DOM parser, and it seems to left unclosed Streams after writing to the XML file.

My program creates a file, and call this code (look below) to write xml-nodes to the file. My program also contains a "delete" file function, which doesn't works because there are some Streams unclosed Streams, so deleting file returns false.

When I rerun the program, I am able to delete the file.. But every time I create a new, or edit in an existing file inside the program, the program seems to left some opened Streams.

This following code is the part that left an unclosed Stream. I tried to somehow close the Streamresult, but there's no method to close it.

public void rewriteXML(){
try{

        DocumentBuilderFactory song = DocumentBuilderFactory.newInstance();
            DocumentBuilder songBuilder = song.newDocumentBuilder();

            Document doc = songBuilder.newDocument();
            Element playlist = doc.createElement("playlist");
            doc.appendChild(playlist);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer tr = tf.newTransformer();
            tr.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource dom = new DOMSource(doc);
            StreamResult sr = new StreamResult(new FileOutputStream(fileName,false));

            tr.transform(dom, sr);

            System.out.println("done");

        }catch(ParserConfigurationException pce){
            pce.printStackTrace();
        }catch(TransformerException fce){
            fce.printStackTrace();
        }catch(IOException ie){
            ie.printStackTrace();
        }
    }

I need some way to either close the Stream, or a way to bug fix for open Streams, because I am not really sure what Stream is unclosed.

解决方案

FileOutputStream is a Stream. StreamResult is not a Stream, but it contains one. The trick is to find it and then close it.

sr.getOutputStream().close()

Michael Kay (comments) pointed out that StreamResult leaves any Stream that is passed to it open. If you did not know this and wanted to be safe, it might be worth noting that most streams in Java do not throw an exception if you attempt to close them twice. If you aren't sure, close it again.

这篇关于Java需要帮助关闭Stream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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