We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Exception in thread "main" java.io.IOException: Stream closed

vgsrikanth
vgsrikanth asked
on
Medium Priority
4,635 Views
Last Modified: 2012-06-27
Hello, I have written the following simple program but I am getting this exception :
 Exception in thread "main" java.io.IOException: Stream closed

Can someone help me to fix this problem.

The program is :

public class Reading_TextFile {
   public static void main(String[] args) throws IOException {
      Reading_TextFile rf = new Reading_TextFile();
      InputStream is = rf.getClass().getResourceAsStream("ReadData.txt");
      BufferedInputStream bis = new BufferedInputStream(is);
      String str = "";
      while((bis.available()) != -1) {
            str  = str + Character.valueOf((char)bis.read());
      }
      System.out.println(str);
      bis.close();
      is.close();      
  }
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You only need to close bis
Awarded 2011
Awarded 2011

Commented:
You close is.close() too early
Awarded 2011
Awarded 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Top Expert 2016

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You only need to close bis

(Since closing bis closes is )

Author

Commented:
I have changed the code and closed only the bis but I am not getting any output.The location of the ReadData.txt is fine and it has data.

Author

Commented:
Thank you Savant for the link.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Please post the code you're using now

Author

Commented:
Hello Savant, the below is the code I am using :

public class Reading_TextFile {
   public static void main(String[] args) throws IOException {
      Reading_TextFile rf = new Reading_TextFile();
      InputStream is = rf.getClass().getResourceAsStream("ReadData.txt");
      BufferedInputStream bis = new BufferedInputStream(is);
      String str = "";
      while((bis.available()) != -1) {
            str  = str + Character.valueOf((char)bis.read());
      }
      System.out.println(str);    
  }
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
You're not closing it at all now. Why don't you just use the code at the link i posted?

Author

Commented:
ok Savant thank you for the link.I shall use it.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
Thank you Savant.Can you give me your email address so that I can contact you in future for any doubts.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Generally speaking, apart from professional issues (where you can contact us via our profiles) we should be contacted in the normal way on this site, i.e. by asking questions

Author

Commented:
ok Savant.Thank you.Just a small request.Can you give me a good link for EJB 3 examples along with connected technologies.
CERTIFIED EXPERT
Top Expert 2016

Commented:
That's a completely different topic and should therefore be the subject matter of a new question
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Thank you Savant.Can you give me your email address so that I can contact you in future for any doubts.

see my profile
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.