Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Asking help for a java io problem

2 views
Skip to first unread message

witk...@163.com

unread,
Jan 17, 2006, 9:30:52 AM1/17/06
to
Hello, guys! I have a problem running the following code with eclipse
showing that:

java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at HtmlFileParser.parseHtmlDoc(HtmlFileParser.java:32)
at HtmlFileParser.main(HtmlFileParser.java:52)

//code here:
try{
FileInputStream fin = new FileInputStream("test.html");
InputStreamReader fr = new InputStreamReader(fin);
for(i=0; i<2048; i++) buf[i] = '0';
fr.read(buf, 0, 2048); //problem here (line 32)
FileWriter fw = new FileWriter("bufWriteTest.txt");
fw.write(buf);
fw.flush();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
There are no syntax erro in the above code, it just dosen't work,
please help me finding out what the problem is, thank you.

Matt Humphrey

unread,
Jan 17, 2006, 1:17:49 PM1/17/06
to

<witk...@163.com> wrote in message
news:1137508252.6...@f14g2000cwb.googlegroups.com...

> Hello, guys! I have a problem running the following code with eclipse
> showing that:
>
> java.io.IOException: Stream closed
> at sun.nio.cs.StreamDecoder.ensureOpen(Unknown Source)
> at sun.nio.cs.StreamDecoder.read(Unknown Source)
> at java.io.InputStreamReader.read(Unknown Source)
> at HtmlFileParser.parseHtmlDoc(HtmlFileParser.java:32)
> at HtmlFileParser.main(HtmlFileParser.java:52)
>
> //code here:
> try{
> FileInputStream fin = new FileInputStream("test.html");
> InputStreamReader fr = new InputStreamReader(fin);
> for(i=0; i<2048; i++) buf[i] = '0';
> fr.read(buf, 0, 2048); //problem here (line 32)

Although your code runs fine for me, there are a couple of issues you
should be aware of. The first is that InputStreamReader converts the
underlying bytes into characters using the default encoding--are you sure
this matches the encoding of the file? My first guess is that they don't
match and that a 2-byte or multi-byte character fails to assemble, which is
why the read closes early.
You also discard the number of bytes read. Although this won't cause your
problem, it means the file writer will pad all your output with zeros upto
the buffer size.

Cheers,
Matt Humphrey ma...@ivizNOSPAM.com http://www.iviz.com/


0 new messages