FileInputStream read()

Hi Guys,The value of -1 returned from the FileInputStream's read() method indicates the end of file.But what if the file being read has "-1" in it ?thanksV
[191 byte] By [vikasphonsa] at [2007-9-30 18:08:51]
# 1
The read methods read individual bytes, each of which is returned to you as an int with a value from 0 to 255, which represents all possible values of a byte. The int value of -1 cannot be read from a byte.
ChuckBing at 2007-7-6 18:40:07 > top of Java-index,Administration Tools,Sun Connection...
# 2
pardon my ignorance. but when u read a byte as primitive java data type of byte the range is -128 to 127 which includes negative values.am I confusing the byte data type with the byte read by the the read() method of fileinputstream.thanks for your help.V
vikasphonsa at 2007-7-6 18:40:07 > top of Java-index,Administration Tools,Sun Connection...
# 3
If the byte value that's read were provided to you as a byte, it would be a signed byte as you say. But the value is placed in the lower 8 bits of an int, which allows values of 0 to 255.
ChuckBing at 2007-7-6 18:40:07 > top of Java-index,Administration Tools,Sun Connection...
# 4

that clears things up for read() in FileInputStream. Another question, in the DataInputStream.readInt() method why can't the return value of -1 be used to indicate the end of file, why does the EOFException needs to be thrown.

why would -1 be a valid value for DataInputStream.readInt() and not for the FileInputStream.read() ( you have explained that though )

Thanks again for your help.

V

vikasphonsa at 2007-7-6 18:40:07 > top of Java-index,Administration Tools,Sun Connection...
# 5

The ReadInt method "Returns: the next four bytes of this input stream, interpreted as an int."

It assumes that the 4 bytes are a valid int (they will be if you're doing things correctly), and an int can have a value of -1, so it can't use that value as EOF.

If you learn how signed/negative numbers are represented at the bit-level, this will make sense to you. Do a search for "two's complement arithmetic", which is how it's done.

ChuckBing at 2007-7-6 18:40:07 > top of Java-index,Administration Tools,Sun Connection...