Skip to main content

RandomAccessFile.readByte() find EOF

3 replies [Last post]
chopficaro
Offline
Joined: 2010-05-01

im trying to create a log file, for which i need to use the RandomAccessFile class for, its the only class i know of which can write a string to a file with 8 bits per char. problem is, the file pointer wants to stay at the beginning of the file. i need to move it manually with seek so that i can log more than one thing. i need to feed seek the length of the file, so i figured id read with RandomAccessFile.readByte() until i reach EOF. however, when readByte reaches EOF, an exception is thrown, which stops my program.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11

STOP SPAMMING this forum with questions unrelated to Swing & AWT.

Jeanette

rturnbull
Offline
Joined: 2005-08-27

> im trying to create a log file, for which i need to
> use the RandomAccessFile class for, its the only
> class i know of which can write a string to a file
> with 8 bits per char. problem is, the file pointer
> wants to stay at the beginning of the file. i need to
> move it manually with seek so that i can log more
> than one thing. i need to feed seek the length of the
> file, so i figured id read with
> RandomAccessFile.readByte() until i reach EOF.
> however, when readByte reaches EOF, an exception is
> thrown, which stops my program.

I must be missing something - can't you catch the exception - surround the read with
a try/catch block.

Also, can't you use the length() method to get the length of the file?

chopficaro
Offline
Joined: 2010-05-01

oops srry
the try catch would end the program, but the length() method fixes everything, ty