• 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

reading -1 from file ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i read the number -1 from the file because end of file is also -1??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you reading the file byte by byte, using the read() method of InputStream? Try using the read(byte[] buffer) method - see the API documentation for more info on how to use this.
 
Ajay Divakaran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im reading byte by byte
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And did using a byte[] buffer help?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajay Divakaran:
im reading byte by byte



Well, the value range for a byte, as returned by an InputStream, is 0-255, it cannot possibly have the value -1. So I'm not sure what you are trying to do, but it's certainly not reading -1.

What do you want to do?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm - the read() method certainly can return -1, but that's different from indicating that there's a -1 in the file. I believe that's the point of the question.

Ajay, how was the -1 stored in the file? Is it a text file? Or some kind of binary format?

If it's a text file, then assuming it uses a common ASCII-like encoding, -1 would be represented with two bytes: a 0x2D (a '-' character) followed by a 0x31 (a '1' character). If that's the sort of file you're dealing with, then I recommend using a Scanner to read the number. Or if you're using an older JDK than 1.5, use a BufferedReader to read a line from the file, and use parseInt() to convert the characters "-1" into a number. You may well need to do additional parsing if there is other data on the same line.

If it's not a text file, then you may want to use a DataInputStream for a variety of methods for converting bytes to primitives. You need to know if the number was written as one byte, two, four, etc.

If the number was written as a single byte in binary format, then you may also be able to convert it using something like this:

Here a "real" -1 would have been read as 255, because that's what binary 11111111 means when you assume a range of 0-255. Casting to byte forces it into the range -128 to 127, which converts 255 to -1.

So Ajay, these methods will all give you different answers. You really need to know how the data was written in the first place in order to parse it correctly. I hope at least one of these possibilities makes sense to you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic