[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1374
  • Last Modified:

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();      
  }
}
0
vgsrikanth
Asked:
vgsrikanth
  • 7
  • 6
  • 2
  • +1
1 Solution
 
CEHJCommented:
You only need to close bis
0
 
for_yanCommented:
You close is.close() too early
0
 
for_yanCommented:
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();      
} 

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
0
 
CEHJCommented:
>>You only need to close bis

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

see my profile
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now