Java Reference
In-Depth Information
bufWriter.write("line two"]);
bufWriter.newLine();
// Close everything.
bufWriter.close();
fileWriter.close();
R ANDOM A CCESS F ILE
The RandomAccessFile class is quite different from the other java.io classes for per-
forming input and output operations. RandomAccessFile does not extend any of the
reader/writer classes or any of the stream classes. It does not extend any other of the
java.io classes. Also, in contrast to the other classes of java.io, it is not specialized for
input or output but is able to do both through its implementation of the DataInput
and DataOutput interfaces.
You can use RandomAccessFile to obtain functionality quite close to the
COBOL functionality of the relative record file. Unlike COBOL, but like the stream
and reader/writer classes, the concept of a logical record is completely lacking out-
side the Java code you create to read and write the data in the file. You might best
think of a RandomAccessFile as a stream that you can read or write from any loca-
tion in the file. (However, about the closest you can get to this notion of a record
would be to set the data array to the size of the record.)
Relative file I/O is not supported by all COBOL compilers and can have differ-
ent implementations on different platforms. Generally, the logic to read a relative
file randomly is something like this:
SELECT CUSTOMER -FILE ASSIGN TO CUSTOMER
ORGANIZATION IS RELATIVE
ACCESS MODE IS RANDOM
RELATIVE KEY IS WW-CUSTOMER-NUMBER.
FD CUSTOMER -FILE.
01 CUSTOMER -RECORD.
05 CUSTOMER-NAME PIC X(25).
05 CUSTOMER-ADDRESS PIC X(30) OCCURS 5 TIMES.
01 WW-CUSTOMER- NUMBER PIC 9(10) VALUE ZEROES.
Search WWH ::




Custom Search