Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Asking help for a java io problem

Thread view: 
witkowski@163.com - 17 Jan 2006 14:30 GMT
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 - 17 Jan 2006 18:17 GMT
> Hello, guys! I have a problem running the following code with eclipse
> showing that:
[quoted text clipped - 12 lines]
>     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 matth@ivizNOSPAM.com  http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.