• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about file class

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic