• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in reading from file

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody.
i faced such problem and can't understand the reason. well, i have a file wich contains text.
and when i'm trying to do this:
for(int i=1;i<=file.available();i++){
......
}
i get only a half of the file contents read.
plz explain why?
would be really appreciated for the help. thanx
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The avilable() method does not necessarily give you all the bytes that are in a file; it gives you the number of bytes that at available at that moment (without waiting). For various reasons, the whole contents of a file may not be available for reading all at once. Maybe the file is fragmented into several pieces at different locations on the hard drive. Or maybe part ofthe system has an internal buffer that is full at the moment, and more bytes can't be read until the currently buffered bytes are cleared. Regardless, to know whether you have really reached the end of the file or not, you need to try to read at least one byte past the amount available(). If the method returns a -1, that indicates an end of file; otherwise, you just wait until more bytes are available. In fact, available() is pretty useless for reading files, since you can never detect an end of file unless you ignore it. Try something like this:

Each time through the loop, you will read either the number of bytes that are available, or the number of bytes that can fit in the buffer, whichever is less. Except the last time, when no bytes are available and you read anyway - the -1 tells you that you're done.
[ July 18, 2002: Message edited by: Jim Yingst ]
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the explanation.
but still how can i make your example work if i have FileInputStream object file?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm... by using the code I showed? When I named a variable "inStream", that was intended to indicate that it was some form of InputStream - such as a FileInputStream, which you have. So you can put your FileInputStream where I wrote "inStream".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic