hi folks! I'm new to this forum and am learning
alot by studying the questions and responses here. Going to take my Prog exam on July 22 ...
To: Deepali Pate from the C:\jdk1.3.1\docs\api\index.html
(comes with JDK and you can install documentation on your computer or else go to Sun web site online docs) public abstract class OutputStream public abstract void write(int b)
throws IOException
"Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream.
The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."
so ...
when you go to read this file with
"System.out.println (raf.readInt());"
you'll find that the documentation for the
RandomAccessFile shows:
"Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
(b1 << 24) | (b2 << 16) (b3 << 8) b4
This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
Returns: the next four bytes of this file, interpreted as an int. so ... You originally "wrote" 10 ints to the file, each stored into 1 byte using the write method of the FileOutputStream Class however, you are reading 4 bytes there instead of 1 byte to try to get at the numbers you stored.