File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RandomAccessFile" Watch "RandomAccessFile" New topic


Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
for the following question
How many bytes are written into the file after line //1?
(Select one correct answer)
public class Test049
public static void main(String args[])
File f = new File("test.txt");
RandomAccessFile r = new RandomAccessFile(f, "rw");
r.writeLong(5); //1
catch(Exception e)

the answer shown is 18 bytes.
But after writing the first char,int,int the total no of bytes written is 2 + 4 + 4 = 10. then we do a seek(4) and then writeLong(5) which is another 8 bytes. What purpose does the seek(4) serve. Doesn't it move the pointer to before the 4th byte and start writing from there so a part of the previous written stuff is erased ??? I am a bit confused with this. Can anyone explain pls
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I would say overwritten rather than erased.
Adding the statement System.out.println(r.length()) results in the output of 12.
So your analysis looks correct.
Where did you find the question?
[ August 16, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
When I run this, it produces a file 12 bytes long.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Rodge Thomas
Ranch Hand

Joined: Jul 25, 2002
Posts: 38
question is from mock exam 2, #49 at:

I agree. Here's the link:
subject: RandomAccessFile
It's not a secret anymore!