Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about file class

 
wei liu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic