This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RAF method.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RAF method...." Watch "RAF method...." New topic
Author

RAF method....

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

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
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
Rob
How do you know that write() method will write only 1 byte ..
??
Sonir
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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
Ok Rob
Will it always write 1, or will it see what is passed into the write() method's argument?..

Sonir
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
Don't get me started about those stupid light bulbs.
 
subject: RAF method....
 
Similar Threads
InputStream to RandomAccessFile..best way?
RandomAccessFile & FileInputStream read
how to getcursor to first location of file?
I have rewritten my mock exam and put it at http://www.jiris.com/
from Sun Guoqiao mock 2