This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RandomAccessFIle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RandomAccessFIle" Watch "RandomAccessFIle" New topic
Author

RandomAccessFIle

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
I have some queries related to RandomAccessFile:
1.What is the need for the class RandomAccessFile when we have FileInputStream and FileOutputStream?
2.The following code produces strange results:
RandomAccessFile raf=new RandomAccessFile("Io.java",rw);
raf.writeUTF("This has been written by RandomAccessFile");
When I execute thid code and open the Io.java file,then it has the following text:
"(This has been written by RandomAccessFile" without quotes.
Now why is there a bracket before the text??
3.When the same program is executed using the method writeChars(),then Io.java contains the following information:
"T h i s h a s b e e n w r i t t e n b y R a n d o m A c c e s s F i l e"
Why is there a space between each character?
4.When we call the seek method of the RandomAccessFile,where does the next read/write operation take place,at the new Position or after the new Position?
5.Can a RandomAccessFile object be closed?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Read the API Spec. Virtually all of your questions can be answered there. If you have a specific question, or don't understand something from the API, then ask.
Corey


SCJP Tipline, etc.
Raj Hans
Greenhorn

Joined: May 10, 2002
Posts: 5
Originally posted by Gautam Sewani:
I have some queries related to RandomAccessFile:
1.What is the need for the class RandomAccessFile when we have FileInputStream and FileOutputStream?
2.The following code produces strange results:
RandomAccessFile raf=new RandomAccessFile("Io.java",rw);
raf.writeUTF("This has been written by RandomAccessFile");
When I execute thid code and open the Io.java file,then it has the following text:
"(This has been written by RandomAccessFile" without quotes.
Now why is there a bracket before the text??
3.When the same program is executed using the method writeChars(),then Io.java contains the following information:
"T h i s h a s b e e n w r i t t e n b y R a n d o m A c c e s s F i l e"
Why is there a space between each character?
4.When we call the seek method of the RandomAccessFile,where does the next read/write operation take place,at the new Position or after the new Position?
5.Can a RandomAccessFile object be closed?

1. RandomAccessFile is useful for non-sequantial read/writes.
2. The bracket comes for the part conversion of your string to UTF-8
3. Your text when converted to UTF-8, the high bit empty and set to space, hence u see spaces b4 each character
4.read and write can start from location u specify in the method, could from the current pointer location, from the beginning, or and offset from the pointer location (remember RAF is used for no-sequential read/write)
5. YEs
 
 
subject: RandomAccessFIle
 
Similar Threads
How to insert new line a text file
JSP behaving badly!
writing text file with RandomAccessFile
How can I change the character in mid of the file
Tomcat madness!