Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

io

 
madhu avula
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you used --
RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
raf.seek( raf.length() - 1);
raf.writeChars("FINAL TEXT");
??
 
madhu avula
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manfred,
thank's for u'r explanation.it's very clear.i got it.
thanks a lot
madhu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic