File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes io 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 "io" Watch "io" New topic
Author

io

madhu avula
Greenhorn

Joined: Apr 24, 2002
Posts: 8
JQ+ Question ID :971005958690

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");

According to me the correct answer is 5.
But answer given in jq+ is 4.
anybody please let me know which is correct?
thanks in advance
madhu
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
What happened when you used --
RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek( raf.length() - 1);
raf.writeChars("FINAL TEXT");
??


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
madhu avula
Greenhorn

Joined: Apr 24, 2002
Posts: 8
marlin,
i tried both options 4 and 5.
both are printing "FINAL TEXT" at end of file.

thanks in advance.
madhu
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Madhu,
You must not have been looking very closely at the results.
Lets show a simple example. The original file.txt contains the following single line of text:
Madhu Avula
Now we run case number 4:

The file now contains the single line of text:
Madhu AvulaFINAL TEXT
So far so good, now we will run the case number 5:

The file now contains the single line of text:
Madhu AvulFINAL TEXT
Something is different here ... the comparison between the two is left as a homework assignment for the student.
Now read the question very carefully:

Which of the following code segments will correctly write the text "FINAL TEXT" to the end of a file "file.txt ?

Regards,
Manfred.
madhu avula
Greenhorn

Joined: Apr 24, 2002
Posts: 8
Hi Manfred,
thank's for u'r explanation.it's very clear.i got it.
thanks a lot
madhu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: io