Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
Yeah, but is it art? What do you think tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic