36.0k views
4 votes
Which of the following declares a valid PrintWriter object that represents an output file named myOutput.txt? PrintWriter out = new File("myOutput.txt"); PrintWriter out = new Scanner("myOutput.txt"); PrintWriter out = new PrintWriter("myOutput.txt"); PrintWriter out = new PrintWriter(new File("myOutput.txt"));

1 Answer

3 votes

Answer:

PrintWriter out = new PrintWriter(new File("myOutput.txt"));

Step-by-step explanation:

PrintWriter object is used to create an abstraction to a File for writing purpose.

PrintWriter constructor with File object input takes one of the two forms:

  • PrintWriter(File)
  • PrintWriter(File,String).

Among the given options, option 4 corresponds to PrintWriter object creation using the first version of PrintWriter constructor.

So the valid option that declares a PrintWriter object for an output file 'myOutput.txt' is:

PrintWriter out = new PrintWriter(new File("myOutput.txt"));

User Ben Ylvisaker
by
5.0k points