|
UBBFriend: Email This Page to Someone! |
JavaRanch
Big Moose Saloon Programmer Certification I/O
|
next newest topic | next oldest topic |
Author | Topic: I/O |
Milind ranch hand |
posted March 14, 2000 08:44 PM
FileOutputStream fos = new FileOutputStream ("XX"); for (byte b =10; b<50;b++) fos.write(b); fos.close(); RandomAccessFile raf = new RandomAccessFile ("xx",
"r"); 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. IP: Logged |
Tony
Alicea sheriff |
posted March 14, 2000 08:47 PM
Explain what? What do you think the answer should be and why? IP: Logged |
Milind ranch hand |
posted March 14, 2000 09:03 PM
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
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
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. [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 |
|
Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation
(formerly Madrona Park, Inc.), 1998 -
2000.