Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:51:07+00:00 2026-05-31T12:51:07+00:00

For code similar to the following: InputStream is = new FileInputstream(test.xml); Document doc =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:51:08+00:00Added an answer on May 31, 2026 at 12:51 pm

    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.

    public class DocumentBuilderTest {
    
     public static void main(String[] args) {
       try {
         InputStream is = new MyInputStream("project.xml");
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
         documentBuilder.parse(is);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
    
     static class MyInputStream extends FileInputStream {   
       public MyInputStream(String filename) throws FileNotFoundException {
         super(filename);
       }
    
       @Override
       public void close() throws IOException {
         // here we log when the stream is close.
         System.out.println("file input stream closed.");
         Exception e = new Exception();
         e.printStackTrace();
         super.close();
       }
    
     }
    }
    

    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:

    at DocumentBuilderTest$MyInputStream.close(DocumentBuilderTest.java:37)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.close(XMLEntityManager.java:3047)
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.close(UTF8Reader.java:661)
    at com.sun.xml.internal.stream.Entity$ScannedEntity.close(Entity.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.endEntity(XMLEntityManager.java:1406)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1763)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1543)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1400)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at DocumentBuilderTest.main(DocumentBuilderTest.java:22)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using code similar to the following: http://blog.flexexamples.com/2007/08/29/launching-new-browser-windows-from-flex/ I have a Flex app running
I have code similar to the following. using (MyEntities context = new MyEntities()) {
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code similar to the following with a URL like this... If I
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>
I've got code similar to the following... <p><label>Do you have buffet facilities?</label> <asp:RadioButtonList ID=blnBuffetMealFacilities:chk
I have Perl code similar to the following: # -- start -- my $res;
I see a lot of code similar to the following var customrs = MyDataContext.Customers.Where(...);
I have a HTML file that has code similar to the following. <table> <tr>
In a CUDA kernel, I have code similar to the following. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.