Link to home
Start Free TrialLog in
Avatar of vgsrikanth
vgsrikanth

asked on

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

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();      
  }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You only need to close bis
You close is.close() too early
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>You only need to close bis

(Since closing bis closes is )
Avatar of vgsrikanth
vgsrikanth

ASKER

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.
Thank you Savant for the link.
Please post the code you're using now
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);    
  }
}
You're not closing it at all now. Why don't you just use the code at the link i posted?
ok Savant thank you for the link.I shall use it.
Thank you Savant.Can you give me your email address so that I can contact you in future for any doubts.
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
ok Savant.Thank you.Just a small request.Can you give me a good link for EJB 3 examples along with connected technologies.
That's a completely different topic and should therefore be the subject matter of a new question
> Thank you Savant.Can you give me your email address so that I can contact you in future for any doubts.

see my profile