posted 24 years ago
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).]