aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQ+ question RandomAccessFile help..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQ+ question RandomAccessFile help....." Watch "JQ+ question RandomAccessFile help....." New topic
Author

JQ+ question RandomAccessFile help.....

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Which of the following code segments will correctly write the text "FINAL TEXT" to the end of a file "file.txt ?
1. RandomAccessFile raf = new RandomAccessFile("file.txt", "a");
raf.writeChars("FINAL TEXT");
2. RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.writeChars("FINAL TEXT");
3. RandomAccessFile raf = new RandomAccessFile("file.txt", "a");
raf.seek( raf.length() );
raf.writeChars("FINAL TEXT");
4. RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek( raf.length() );
raf.writeChars("FINAL TEXT");
5. RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek( raf.length() - 1);
raf.writeChars("FINAL TEXT");
The correct answer is apparently 4)
However, I'm sure I've read that raf.length() - 1) will point to the end of the file in RHE...........then then I haven't slept in 48 hours so I may just be delirious..........
Any help would be much appreciated,
Rowan.


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
(raf.length() - 1) is the position of the last character in the file. If you write to this position, you will overwrite the character that was previously last with a "F". If you want to write the string after all existing text, you need to position the file pointer after the last character.


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQ+ question RandomAccessFile help.....