Which code fragment correctly appends “Java 7” to the end of the file /tmp/msg.txt?
A.
FileWriter w = new FileWriter(“/tmp/msg.txt”);
append(“Java 7”);
close();
B.
FileWriter w = new FileWriter(“/tmp/msg.txt”, true);
append(“Java 7”);
close();
C.
FileWriter w = new FileWriter(“/tmp/msg.txt”, FileWriter.MODE_APPEND);
append(“Java 7”);
close();
D.
FileWriter w = new FileWriter(“/tmp/msg.txt”, Writer.MODE_APPEND);
append(“Java 7”);
close();
Explanation:
FileWriter(File file, boolean append)
A: clears the file and append “Java7”
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 toappend – if true, then bytes will be written to the end of the file rather
than the beginning
Answer is B, the boolean true is to say that is possible to append.
B