If you click on this stuff and give your money to some city slickers, JavaRanch gets a kickback. Hoooooooo Doggy!     
Do not post questions from the actual certification exams. If you see a question from an actual exam, please e-mail the moderators privately to let us know. To discuss a question from a mock exam, please mention which mock exam and the question number (if provided) so we know it's not from the real exam.



UBBFriend: Email This Page to Someone!
  JavaRanch Big Moose Saloon
  Programmer Certification
  I/O

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   I/O
Milind
ranch hand
posted March 14, 2000 08:44 PM     Click Here to See the Profile for Milind   Click Here to Email Milind     Edit/Delete Message Reply w/Quote
FileOutputStream fos = new FileOutputStream ("XX");
for (byte b =10; b<50;b++)
fos.write(b);
fos.close();

RandomAccessFile raf = new RandomAccessFile ("xx", "r");
raf.seek(10);
int i = raf.read();
raf.close();
System.out.println ("i ="+i);

Ans :

The output is i = 20

Can anyone explain this? I have started working on this topic but it seems that it's quite confusing.
Regds,
Milind

IP: Logged

Tony Alicea
sheriff
posted March 14, 2000 08:47 PM     Click Here to See the Profile for Tony Alicea   Click Here to Email Tony Alicea     Edit/Delete Message Reply w/Quote
Explain what? What do you think the answer should be and why?

IP: Logged

Milind
ranch hand
posted March 14, 2000 09:03 PM     Click Here to See the Profile for Milind   Click Here to Email Milind     Edit/Delete Message Reply w/Quote
Hi Tony,
I feel that FileOutStream has no consequence here.
seek() method is being used to set the current position of file (which is 10 in this case). Now read returns the next byte from the file. I am not not sure about the rest.Can u please help me?
Regds,
Milind

IP: Logged

Jim Yingst
sheriff
posted March 14, 2000 10:09 PM     Click Here to See the Profile for Jim Yingst   Click Here to Email Jim Yingst     Edit/Delete Message Reply w/Quote
The FileOutputStream does have consequence - it is what is used to create the file "XX" and put data into it. (I'm assuming that both "xx" and "XX" were intended to refer to the same file - they won't on a Unix system unless the case of one is changed to match the other.) Anyway, you need to figure out exactly what the FileOutputStream writes to the file, and specifically, what is in the 11th byte (i.e. position 10, starting from 0). That's what is read by the read() statement, and printed.

IP: Logged

maha anna
bartender
posted March 14, 2000 11:22 PM     Click Here to See the Profile for maha anna   Click Here to Email maha anna     Edit/Delete Message Reply w/Quote
Miland,
The overall function of this code is , a new file 'xx' is opened/created if it does not already exist, in the current dir. Then the byte values of numbers 10,11,12....49 are written as only 1 byte(the last 8 bits). Note that the 'fos.write(b);' statement has the form of 'fileOutPutStreamObject.write(int i);' Basically what it does is the input byte b in our case is promoted to an 'int' (4 bytes) and the last byte(only last 8 bits)is written to the fos, which in turn is connected to the physical file 'xx' and so is written to 'xx' file. Also note that FileOutputStream/FileInputStream are byte oriented. They can read/write only in terms of bytes.

The RandomAccess file opens this file for reading only.A file's file pointer starts at 0 position. So when you seek (here we can say this is advance,unlike 'C' where you can move backward also by giving -ve arg to seek) 10 positions , you are pointing at the 11th byte. Since we started writing from 10,11,12,13...49 in xx using fos before, now the 11th byte is number 20. So it prints 20. Also note that RandomAccessFile's int read() method is same as int fileInputStreamObject.read() method. But in FileInputStream you can't just like that seek to a particular position and read the byte which you want only. In fis ,you have to read from the starting in byte by byte basis.
regds
maha anna

[This message has been edited by maha anna (edited March 14, 2000).]

IP: Logged

All times are MT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | JavaRanch

Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.