Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFIle

 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj Hans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic