Which code fragment correctly appends "Java 7" to the end of the file /tmp/msg.txt?

Which code fragment correctly appends “Java 7” to the end of the file /tmp/msg.txt?

Which code fragment correctly appends “Java 7” to the end of the file /tmp/msg.txt?

A.
Filewriter w = new FileWriter(“/tmp/msg.txt”);

B.
append(“Java 7”);

C.
close();

D.
Filewriter w = new FileWriter(“/tmp/msg.txt”, true);

E.
append(“Java 7”);

F.
close();

G.
Filewriter w = new FileWriter(“/tmp/msg.txt”, FileWriter.MODE_APPEND);

H.
append(“Java 7”);

I.
close();

J.
Filewriter w = new FileWriter(“/tmp/msg.txt”, Writer.MODE_APPEND);
K.
append(“Java 7”);
L.
close();

Explanation:
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
Parameters:
file – a File object to write to
append – if true, then bytes will be written to the end of the file rather than the beginning



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Ray

Ray

B. should in this way

FileWriter w = new FileWriter(“/tmp/msg.txt”, true);
w.append(“Java 7″);
close();

Charlie

Charlie

FileWriter w = new FileWriter(“/tmp/msg.txt”, true);
w.append(“Java 7″);
close();

or

FileWriter w = new FileWriter(“/tmp/msg.txt”, true);
w.write(“Java 7″);
close();

Both can work. But

FileWriter w = new FileWriter(“/tmp/msg.txt”);
w.append(“Java 7″);
close();

does not work.

hafid

hafid

The right question is :

Which code fragment correctly appends “Java 7″ to the end of the file /tmp/msg.txt?

A.
Filewriter w = new FileWriter(“/tmp/msg.txt”);
w.append(“Java 7″);
w.close();

B.
Filewriter w = new FileWriter(“/tmp/msg.txt”, true);
w.append(“Java 7″);
w.close();

C.
Filewriter w = new FileWriter(“/tmp/msg.txt”, FileWriter.MODE_APPEND);
w.append(“Java 7″);
w.close();

D.
Filewriter w = new FileWriter(“/tmp/msg.txt”, Writer.MODE_APPEND);
w.append(“Java 7″);
w.close();

Answer: B
Explanations:
If the second argument is true, then bytes will be written to the end of the file rather than the beginning