jQuery in Action, 3rd edition
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Performing read/write to the file : how Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Performing read/write to the file : how" Watch "Performing read/write to the file : how" New topic

Performing read/write to the file : how

Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167

I wanted to know what is the recommended way to read/write text from the file.

I see the instructions say numeric data is read/written as DataInputStream while text data is read/stored contain only 8 bit character.

Should I be using RandomAccessFile or something else.

What is the recommended way of performing this.

Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Yep, I believe we should be using the RandomAccessFile for it. Take a look at the tutorial available from Sun.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
I am not sure how the files should be stored as.
After the header, should the data be stored as plain text.

Java uses UTF-16 as default encoding, and on opening the file when generated using UTF-16, it is comes like boxes/chars.

I am trying to use US-ASCII to write to the file, but the generate file doesnt seem to contain plain text.
I am using RandomAccessFile.

Here is like a sample code, I am writing.

Please help


raf.writeChars(new String(lr.location.getBytes("US-ASCII"),"US-ASCII"));
raf.writeChars(new String(lr.rate.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.smoking.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.date.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.owner.getBytes(),"US-ASCII"));
Consider Paul's rocket mass heater.
subject: Performing read/write to the file : how
It's not a secret anymore!