Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RandomAccessFile" Watch "RandomAccessFile" New topic
Author

RandomAccessFile

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)
-----------------------------------------------------------------------
import java.io.*;
public class Test049
{
public static void main(String args[])
{
try
{
File f = new File("test.txt");
RandomAccessFile r = new RandomAccessFile(f, "rw");
r.writeChar((char)1);
r.writeInt(2);
r.writeInt(3);
r.seek(4);
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
Thanx
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?
-Barry
[ 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 JIRIS.com mock exam 2, #49 at:
http://www.jiris.com/scjp2/mockexam2answer.html


SCJP, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile