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: 9033
10
posted
0
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
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.