1

I ran FindBugs to check my code and it complains that method may fail to close stream:

Properties prop = new Properties();
prop.load(new FileInputStream("file.txt"));
...

Is it a mistake or just false positive? Will this stream be properly closed?

2
  • ... and where this stream is being closed? Perhaps when garbage collected only... So FindBugs is right. Commented Jul 12, 2011 at 20:10
  • I agree that it is not closed, but its scope is limited enough so should I really care? code looks a lot cleaner Commented Jul 12, 2011 at 20:45

2 Answers 2

4

Stream handling is tedious (prior to Java 7). Before that you have to manually close the stream.

InputStream is = null;
try {
   is = new FileInputStream(..);
   // do something with stream
} finally {
   try {
      is.close();
   } catch (Exception ex){ 
      //report problem
   }
}

apache commons-lang can shorten the finally clause by its IOUtils.closeQuitely(is), but note that it hides the exception

Sign up to request clarification or add additional context in comments.

Comments

2

FindBugs is correct, the stream will remain open (at least until the end of the program or it is garbage collected). The stream tyou pass to the the load() method is not closed as the API states.

See: http://download.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.InputStream%29

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.