简体   繁体   English

我是否需要手动关闭输入流?

[英]Do I need to close Input Stream manually?

I'm using Xerces library for parsing XML. 我正在使用Xerces库来解析XML。 Here is the code snippet for parsing: 以下是用于解析的代码段:

Document doc = builder.parse(new InputSource(new StringReader(someXMLString))); Document doc = builder.parse(new InputSource(new StringReader(someXMLString)));

Do I need to close the InputStream in InputSource manually or will the parse method handle it? 我是否需要手动关闭InputSourceInputStream或者parse方法是否会处理它?

Given that you've got no external resources - just a StringReader - you don't need to close it. 鉴于你没有外部资源 - 只是一个StringReader - 你不需要关闭它。 I would do so anyway though... then if you ever change the code to use a different input, you won't accidentally have a resource leak. 无论如何我会这样做...然后如果你改变代码使用不同的输入,你不会意外地有资源泄漏。

(For just throwaway code, I'd leave it - but be aware that if you're not careful, throwaway code has a habit of living longer than expected.) (对于一次性代码,我会离开它 - 但请注意,如果你不小心,一次性代码有一个比预期更长寿的习惯。)

It seems there is nothing in DocumentBuilder API about it. 似乎DocumentBuilder API中没有关于它的内容。 We can test it as 我们可以测试它

InputStream is = new FileInputStream("test.xml") {
    @Override
    public void close() throws IOException {
        System.out.println("close");
        super.close();
    }
};
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
documentBuilder.parse(is);

it prints close . 它打印得很close But since there's nothing in API about it, this behaviour is not guaranteed. 但由于API中没有任何内容,因此无法保证此行为。

You should close it by yourself. 你应该自己关闭它。

The builder.parse method doesn't close the stream in its implementation. builder.parse方法不会在其实现中关闭流。 It won't know when is suitable to close the stream, so this requires the stream being manually closed. 它不知道何时适合关闭流,因此这需要手动关闭流。

If you use try-with then you can let java close it for you. 如果您使用try-with,那么您可以让java为您关闭它。 Otherwise I would recommend closing it manually. 否则我建议手动关闭它。

InputStream supposed to be closed, since close() method: 应该关闭InputStream ,因为close()方法:

Closes this input stream and releases any system resources associated with the stream. 关闭此输入流并释放与该流关联的所有系统资源。

It's doesn't matter is your stream uses resources or not - it's good way to close it. 你的流是否使用资源并不重要 - 这是关闭它的好方法 Just to be sure. 只是要确定。

InputSource只是流上的一个方便的类包装器,因此它不会关闭其中的流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用commons-io的IOUtils.toString(输入)后,是否需要手动关闭输入流? - Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io? 我需要关闭流吗? - Do I need to close a stream? 您是否需要明确关闭Java KeyStore输入流? - Do you need to explicity close a Java KeyStore input stream? 我是否需要在 Java 中手动关闭文件(使用路径打开)? - Do I need to close a file (opened using Paths) manually in Java? 我是否需要从Android&中的FileReader()关闭流? - Do I need to close the stream from FileReader() in Android&? 休眠:您是否需要使用sessionFactory手动关闭? - Hibernate: Do you need to manually close with sessionFactory? 我是否需要在 spring boot data jpa + hibernate 中手动关闭连接 - Do I need to close connection manually in spring boot data jpa + hibernate 我需要保留所有作者/读者的变量以手动将其全部关闭吗? - Do I need to keep variables for all writers/readers to close them all manually? 如果我关闭ObjectOuputStream,那么是否不需要关闭FileOutputStream? - If I close ObjectOuputStream, then do not need to close FileOutputStream? 我是否需要关闭ByteArrayInputStream? - Do I need to close a ByteArrayInputStream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM