*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RandomAccessFile question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RandomAccessFile question" Watch "RandomAccessFile question" New topic
Author

RandomAccessFile question

Archana Agrawal
Greenhorn

Joined: May 14, 2002
Posts: 23
hello,
This is the question from the javacaps .

17. What is the output displayed by the following code?
import java.io.*;
public class TestIPApp {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
[a] 123456
[b] 7890
[c] 1000000
[d] .0001
the answer given is b.7890
I know the concept of randomaccessfile,but i am not getting why this is the output.
please help,
archana sunarcha@yahoo.com
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The first write writes out one byte of data, representing the boolean value true. The next write writes out 4 bytes of data for the int, 123456. The next write writes out 4 more bytes of data for the value 7890. This behavior continues.
When we perform the seek, we seek past the first 5 bytes (the boolean and the first int) and read the bytes 6-9. These 4 bytes contain the value 7890.
That's why we get that value. What are you confused about?
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile question
 
Similar Threads
#60 FROM JAVA CAP
RAF question from javacaps mock, help requested
seek method in RandomAccessFile
Question about RandomAccessFile (1.2 exam)
about file class