File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Performing read/write to the file : how

Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
Hi,

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.

Regards
Aruneesh
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.

Cheers!


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.seek(length);

raf.writeChars(new String(lr.location.getBytes("US-ASCII"),"US-ASCII"));
raf.writeInt(lr.size);
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"));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performing read/write to the file : how