This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading -1 from file ?  RSS feed

 
Ajay Divakaran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i read the number -1 from the file because end of file is also -1??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
im reading byte by byte
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did using a byte[] buffer help?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!