The output is : 0 1 I am wondering from where did this 0 1 come from..Any explainations ?? Sonir
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
System.out.print(f.length + " "); This prints the length of the file f in bytes. Since you just created a brand new file name, the file is empty and is zero bytes long. That's where the first 0 comes from. Then you write a byte to the file: ra.write(10); And you print the new length: System.out.println(ra.length()); Since you just wrote 1 byte, it's now 1 byte long. That's where the 1 comes from. Rob
Rob
SCJP 1.4
sonir shah
Ranch Hand
Joined: Nov 01, 2001
Posts: 435
posted
0
Rob How do you know that write() method will write only 1 byte .. ?? Sonir
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
Because the JavaDoc for RandomAccessFile tells me so. public void write(int b) throws IOException Writes the specified byte to this file. The write starts at the current file pointer. JavaDoc is indespensible! It's the offial documentation for all the Java class libraries.
Rob
sonir shah
Ranch Hand
Joined: Nov 01, 2001
Posts: 435
posted
0
Ok Rob Will it always write 1, or will it see what is passed into the write() method's argument?..
Sonir, The program will always write 1 byte. It does not always write 1. The actual value written will be the 8 low order bits (the rightmost bits) of the value passed to write(). You need to dig into the JavaDocs a little to find this information (I'll leave this to you as an exercise -- which BTW involves mainly just clicking on links and reading). The program outputs 1 because it is printing the value of ra.length(), not the actual value written.