GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about file class 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 "about file class" Watch "about file class" New topic
Author

about file class

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
i dont know why the answer is b? what is function of "file.seek(5)"?

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
Answer 17
[b] 7890
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
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
Hi,
I will try to explain as much as I know, if anybody can clear my ideas, its well and good. If my explanition is not proper please excuze me. Even am beginner.
The Answer is B.
The reason is
When the file is first created, the file pointer is 0, indicating the beginning of the file. Calls to the readXXX and writeXXX methods adjust the file pointer by the number of bytes read or written.
In the above program the fileponter will be at the position 0 which is boolean, from there the number of bytes is calculated. Next is int which is 4 bytes. So the file pointer advances to 4 positons. And when we call seek(5), the poniter will be at next integer written i.e 7890. And hence the result byte. The seek method moves the file pointer according to bytes read or written.
Also I have a question, instead of file.readInt(), if we call file.readLong() is called, it gives diffrent answer why it is so
[This message has been edited by Arathi Rajashekar (edited December 14, 2001).]


Arathi<br />Sun Certified Java Programmer
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Here is what the API doc has to say about readLong..
Reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where:
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,

then the result is equal to:

((long)b1 << 56) + ((long)b2 << 48)
+ ((long)b3 << 40) + ((long)b4 << 32)
+ ((long)b5 << 24) + ((long)b6 << 16)
+ ((long)b7 << 8) + b8

This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.

Hope this helps..
Originally posted by Arathi Rajashekar:
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
Hi,
I will try to explain as much as I know, if anybody can clear my ideas, its well and good. If my explanition is not proper please excuze me. Even am beginner.
The Answer is B.
The reason is
When the file is first created, the file pointer is 0, indicating the beginning of the file. Calls to the readXXX and writeXXX methods adjust the file pointer by the number of bytes read or written.
In the above program the fileponter will be at the position 0 which is boolean, from there the number of bytes is calculated. Next is int which is 4 bytes. So the file pointer advances to 4 positons. And when we call seek(5), the poniter will be at next integer written i.e 7890. And hence the result byte. The seek method moves the file pointer according to bytes read or written.
Also I have a question, instead of file.readInt(), if we call file.readLong() is called, it gives diffrent answer why it is so
[This message has been edited by Arathi Rajashekar (edited December 14, 2001).]

 
wood burning stoves
 
subject: about file class