Question
Consider the following program. What is the purpose of pw? import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class FileUtil { public void writeLinesToFile(String filename, String[]
Consider the following program. What is the purpose of pw?
import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class FileUtil { public void writeLinesToFile(String filename, String[] linesToWrite, boolean appendToFile) { PrintWriter pw = null; try { if (appendToFile) { //If the file already exists, start writing at the end of it. pw = new PrintWriter(new FileWriter(filename,true)); } else { pw = new PrintWriter(new FileWriter(filename)); //this is equal to: //pw = new PrintWriter(new FileWriter(filename, false)); } for (int i = 0; i < linesToWrite.length; i++) { pw.println(linesToWrite[i]); } pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //Close the PrintWriter if (pw != null) pw.close(); } } public static void main(String[] args) { // } }
a) pw is an object of the PrintWriter class. It is used to handle data reading and writing for this program, specifically to read each line of a file and write those lines to the screen.
b) pw is an array that has as its data type a PrintWriter object. It is used to hold data that will ultimately be written to a file.
c) pw is an object of the PrintWriter class. It is used to handle file writing for this program, specifically to write each line of an array to a file.
d) pw is a method of the PrintWriter class. It is used to handle file reading and writing for this program, specifically to write each line from one file to another file.
a, b, c or d?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started