• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

RandomAccessFIle

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic