File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RAF method....

 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output is : 0 1
I am wondering from where did this 0 1 come from..Any explainations ??
Sonir
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob
How do you know that write() method will write only 1 byte ..
??
Sonir
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Rob
Will it always write 1, or will it see what is passed into the write() method's argument?..

Sonir
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic