• 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

I/O

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explain what? What do you think the answer should be and why?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic