aspose file tools*
The moose likes I/O and Streams and the fly likes I'm getting a really weird IOException... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "I Watch "I New topic
Author

I'm getting a really weird IOException...

Acoyani Garrido Sandoval
Greenhorn

Joined: Sep 09, 2007
Posts: 3
OK, so I have this program, which reads two characters, appends them to a StringBuffer, adds the resulting string to a Vector, and does the same until the end of file is reached.

Now, since I didn't managed to remember or find anything on the documentation about an End Of File flag (I remember you can see if you reached it on C with the feof(An input stream) function), I found that I can use the available() function from the FileInputStream class, which returns how much bytes are between the file pointer and the EOF marker. So, I have a While loop like this:


The problem? This code, compiled with JCreator 3.50 LE, worked yesterday in my computer, but it doesn't works anymore! When I run my program, I get the following stack dump (BTW, I think "Controlador no v�lido" translates to "Invalid controller" or something; I've never seen the English version since my computer is in Spanish):


Any idea on what could be going on?
[ September 29, 2007: Message edited by: Acoyani Garrido Sandoval ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I don't know what's the cause of this exception. However, the available() method doesn't do what you think it does, and there's really no reason to use it at all here.

[AGS]: found that I can use the available() function from the FileInputStream class, which returns how much bytes are between the file pointer and the EOF marker.

No - read what the API for this method says:
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.

[ emphasis added by me ]

It's possible for available() to return less than the total bytes remaining in the file - it may even return 0 - simply because those bytes are not available right now. For example, the disc drive may be out of position it the moment, and it will take a few milliseconds to position it.

If you want to detect the end of a file, don't use available(). In fact it's hard to imagine anything that available() is really good for - I advise ignoring it in general. Instead, you can look at what the API for read() says:
Returns:
the next byte of data, or -1 if the end of the file is reached./quote]
If you want to know whether the end of a file has been reached, just check if read() returned a -1. If it didn't, then the file still has data.


"I'm not back." - Bill Harding, Twister
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

just read the input character into a byte and check for -1.
available() is rarely useful.

available() can be useful when you are reading across a network and want to know if you should sleep a bit more to wait for more input. But its not an EOF indicator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm getting a really weird IOException...