• 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

#60 FROM JAVA CAP

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
60. What is the output displayed by the following code?
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
C) 1000000
D) .0001
could anyone explain the o/p?
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is 7890, with a try catch block, for IOException, of course. - that is simply reading an int that was written on to the file earlier i guess !
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is why the output is 7890:
file.seek(5); /* this statement moves the pointer to the beginning of the 5th byte i.e current + d bytes where d in this case is 5, current position is at 0 */
- note that boolean does not have a bit width. So the write Boolean doesn't count.
- the next writeInt is 4 bytes long, Seek skips these 4 bytes and postions the pointer at the 5th byte which is the start of the next integer which is your 7890.
Hope this helps.
Latha
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks latha... but how do we konw the length in bytes...
imean, how did you say 123456 is 4 bytes long?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain why the boolean value did not occupy any space.
I thought boolean values used 1 bit for the true of false.
I understand that an integer is always 32 bits in Java. and that would make 4 bytes.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess that the boolean is treated as one byte.
the FilePointer moves the 5 bytes with 1 byte from the boolean and 4 bytes from the integer.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the smallest unit of information that can be read or written to a file? I assume that the smallest unit is a byte. So I think that writing a boolean to a file will actually mean writting a byte.
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the boolean definitely takes up 1 byte, for if u comment that line out, the o/p is some : 2019840, and then if u change the seek ptr to 4 u get 7890 back. it's upon us to imagine the ptr position beginning at 0 or that the next byte is written from the last position onwards !
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I definitely think a boolean value takes a bytes. I confirmed it with following code:
import java.io.*;
public class TestIPApp {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test3.txt", "rw");
System.out.println(file.getFilePointer() ); //0
file.writeBoolean(true);
System.out.println(file.getFilePointer() ); //1
System.out.println(file.length() );//1
file.writeInt(123456);
System.out.println(file.getFilePointer() );//5
System.out.println(file.length() );//5
file.writeInt(7890);
System.out.println(file.getFilePointer() );//9
System.out.println(file.length() );//9
file.writeLong(1000000);
System.out.println(file.getFilePointer() );
System.out.println(file.length() );
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());//7890
file.close();
}
}
I think the key is how we understand seek(int offset) method. seek(5) actually the next written/read byte is at position 6, like in array, index 5, because the first byte is at position 0.
Judy
 
Latha Kalaga
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Anshuman, I had disappeared. My ISP was down. Anyway, I guess by now you have the answer already. If else what I meant was that since that line is doing a writeInt and int occupies 4 bytes, I said that is how much 123456 will occupy.
Latha

Originally posted by Anshuman Acharya:
thanks latha... but how do we konw the length in bytes...
imean, how did you say 123456 is 4 bytes long?


 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody wrote this:
the boolean definitely takes up 1 byte, for if u comment that line out, the o/p is some : 2019840, and then if u change the seek ptr to 4 u get 7890 back.
So what is the junk value getting printed out?. Is it significant?. I tried to find out how it is getting printed. But not successful. I think it is some memory address. Shall we get such questions in the exam?
thanx.
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the value that is getting printed i.e. 2019840, is not a junk value. it is the decimal value of an integer formed from 3 least significant bytes of 7890 and 1 most significant byte of the next long value, 1000000.
when u set the seek value to 5, and the boolean statement is commented out, this is the integer formed by the next 4 bytes.
[This message has been edited by Sweekriti Engineer (edited February 01, 2001).]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
When i try to compile to code it gives a type error on line 4.
the code is as follows
import java.io.*;
public class Test {
try {
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();
}
}
catch(FileNotFoundException e) {
System.out.println(e);
}
}
what is my mistake.please explain.Thanks in advance.
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please put the try-catch block inside the main method. Secondly u must catch IOException.
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swwekriti engineer,
Thanks.
I too thought the same line. But i didn't build upon that after getting an output of 256 with the foll. code. Sure the junk is not a big value but how do i get that number?. Can u please explain me with the simple numbers i have tried below:
import java.io.*;
public class Randfile {
public static void main(String args[]) throws IOException{
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
//file.writeBoolean(true);
file.writeInt(0);
file.writeInt(1);
file.writeInt(0);
file.writeInt(77);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
o/p:256
thanks.
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
file.writeInt(0);
file.writeInt(1);
file.writeInt(0);
file.writeInt(77);
file.writeFloat(.0001f);
file.seek(5);
your nos. are written onto the file as:
00000000 00000000 00000000 00000000 --0
00000000 00000000 00000000 00000001 --1
00000000 00000000 00000000 00000000 --0
00000000 00000000 00000000 01001101 --77
if u count from left to right 4 bytes from 5th pointer position are 00000000 00000000 00000001 00000000 which is 256 in decimal.
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot. Thats a beauty.
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
i have done some changes to the code so that you can actually see the bit pattern emerging due to writing
first i wrote the file
and then read it byte by byte and printed the byte pattern
THE BOOLEAN HAS TO TAKE SOME SIZE SO THAT IT CAN STORE THE VALUE
AND IT IS ONE BYTE(THE SAMLLEST THING A FILE CAN WRITE!!)
- I will also like to tell you that
seek will act like a cursor so when you say seek(5)
the next thing read will be 6th byte and not the 5th
coz counting starts from 0
i hope it will help

'

'
------------------
KS

[This message has been edited by kalpesh soni (edited February 03, 2001).]
[This message has been edited by kalpesh soni (edited February 03, 2001).]
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic