17.3k views
4 votes
Write an expression whose value is a reference to a newly created PrintWriter object associated with a file named "message.log". The b>PrintWriter object should be created so that existing content in the file is not destroyed or overwritten, but rather output will be appended to any existing content.(Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

User Abhisekp
by
5.7k points

2 Answers

4 votes

Final answer:

To create a PrintWriter that appends to "message.log", instantiate it with a FileWriter object constructed with the filename and true parameter.

Step-by-step explanation:

To create a PrintWriter object in Java that appends to a file instead of overwriting it, you must use the FileWriter class in conjunction with the PrintWriter, specifying that you want to append to the file. The expression you would write to achieve this would be:

PrintWriter pw = new PrintWriter(new FileWriter("message.log", true));

This creates a PrintWriter object pw that wraps around a FileWriter object. The "true" parameter in the FileWriter constructor indicates that you intend to append data to the file "message.log" rather than starting with an empty file.

User Chrisguitarguy
by
5.5k points
4 votes

Answer:

The expression of the following question are:

new PrintWriter("message.log")

Step-by-step explanation:

In the given statement, the value is the reference of the newly formed function's objects that linked to a file called 'message.log.' The b > PrintWriter object can be formed which is not to destroy or overwrite existing content in the file, but rather than to add result to any existing content. So, that's why firstly, we write "new" built-in keyword then, PrintWriter and pass that file in it.

User Bluetech
by
5.7k points