• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

i/o question any help

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
public class TestIPApp {
public static void main(String args[]) {
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();
}
}
Select correct answer:
A) 123456
B) 7890 //how this is answer
C) 1000000
D) .0001
i m unable to figure out why answer is b any help
thanx
kashif
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First you are writing a boolean value which is 1 byte in length and now your file pointer is in the first position.
Next you are writing an int value which is 4 bytes Now your file pointer is in the 5th position.
Now in the fifth position again you are writing the int value
7890.
Now you are in the 9th position you are writing the Long value.
it's advancing like this. when you position the file pionter at 5 ,obviously 7890 will be printed.
Try position at 9 through seek. Long will be printed.
Priya

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sohail,

First your code will not compile. As RandomAccessFile constructor, seek() and close() method in your program throw IOException. You have to either catch them in try catch block ot declare in throws clause of main() method.
On the Assumtion that code will compile correctly, explanation given by Priya is correct. I would like to little more for your clarification:
After a random-access file is constructed, you can seek to any byte position within the file and then read or write. Pre-Java system (the C standard I/O library, for example) have supported seeking to a position relative to the beginning of the file, the end of the file, or the current position within the file. Java's random-access files only support seeking relative to the beginning of the file, but there are methods that report the current position and the length of the file, so you can effectively perform the other kinds of seek as long as you are willing to do the arithmetic.
void seek(long position) throws IOException:
This sets the current position within the file, in bytes. Subsequent reading and writing will take place starting at this position. Files start at position 0.
Priya,
I dont agree with you on this point:
Try position at 9 through seek. Long will be printed.
file.seek(9);
System.out.println(file.readInt());
It will give runtime error as your trying to read long value with
readInt() method. The code will have to be modified to:
file.seek(9);
System.out.println(file.readLong());

------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sohail,

First your code will not compile. As RandomAccessFile constructor, seek() and close() method in your program throw IOException. You have to either catch them in try catch block ot declare in throws clause of main() method.
On the Assumtion that code will compile correctly, explanation given by Priya is correct. I would like to little more for your clarification:
After a random-access file is constructed, you can seek to any byte position within the file and then read or write. Pre-Java system (the C standard I/O library, for example) have supported seeking to a position relative to the beginning of the file, the end of the file, or the current position within the file. Java's random-access files only support seeking relative to the beginning of the file, but there are methods that report the current position and the length of the file, so you can effectively perform the other kinds of seek as long as you are willing to do the arithmetic.
void seek(long position) throws IOException:
This sets the current position within the file, in bytes. Subsequent reading and writing will take place starting at this position. Files start at position 0.
Priya,
I dont agree with you on this point:
Try position at 9 through seek. Long will be printed.
file.seek(9);
System.out.println(file.readInt());
It will give runtime error as your trying to read long value with
readInt() method. The code will have to be modified to:
file.seek(9);
System.out.println(file.readLong());

------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic