• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic