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().getResourceA sStream("R eadData.tx t");
BufferedInputStream bis = new BufferedInputStream(is);
String str = "";
while((bis.available()) != -1) {
str = str + Character.valueOf((char)bi s.read());
}
System.out.println(str);
bis.close();
is.close();
}
}
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().getResourceA
BufferedInputStream bis = new BufferedInputStream(is);
String str = "";
while((bis.available()) != -1) {
str = str + Character.valueOf((char)bi
}
System.out.println(str);
bis.close();
is.close();
}
}
You only need to close bis
You close is.close() too early
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To read a file into a String:
http://technojeeves.com/joomla/index.php/free/93-file-to-string-in-java
http://technojeeves.com/joomla/index.php/free/93-file-to-string-in-java
>>You only need to close bis
(Since closing bis closes is )
(Since closing bis closes is )
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.
ASKER
Thank you Savant for the link.
Please post the code you're using now
ASKER
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().getResourceA sStream("R eadData.tx t");
BufferedInputStream bis = new BufferedInputStream(is);
String str = "";
while((bis.available()) != -1) {
str = str + Character.valueOf((char)bi s.read());
}
System.out.println(str);
}
}
public class Reading_TextFile {
public static void main(String[] args) throws IOException {
Reading_TextFile rf = new Reading_TextFile();
InputStream is = rf.getClass().getResourceA
BufferedInputStream bis = new BufferedInputStream(is);
String str = "";
while((bis.available()) != -1) {
str = str + Character.valueOf((char)bi
}
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?
ASKER
ok Savant thank you for the link.I shall use it.
ASKER
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
ASKER
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
see my profile