For code similar to the following:
InputStream is = new FileInputstream("test.xml");
Document doc = DocumentBuilder.parser(is);
My question is whether I need to close the stream manually (invoke is.close()). Does DocumentBuilder close the InputStream for me?
Use the following test code to see whether the input stream is closed or not, and you could see which line of code closes the stream.
Whether the input stream passed to the DocumentBuilder is closed or not depends on the DOMParser implementation. In my environment, the file input stream is closed, see the stack trace below:
So you could not manually close the stream in this specific example. However, it is always a good idea to close input stream when you are sure that the stream will not be used any more. In your case, once the document is parsed, the input stream is no longer needed, so the stream could be safely closed, and I suggest you do that.