Examine the following file processing program. import java.io.File; import java.
ID: 3821114 • Letter: E
Question
Examine the following file processing program.
import java.io.File;
import java.io.PrintWriter;
public class TextFile {
public static void main(String[] args) throws Exception {
// associate a file with a file object
File fileExample = new File("data.txt");
// associate the file with a PrintWriter object
PrintWriter pwInput = new PrintWriter(fileExample);
// write string data to the file
pwInput.println("Sammy Student");
// write string data to the file
pwInput.println("Engineer");
// write a double - precision number to the file
pwInput.println(35.50);
// write a Boolean value to the file
pwInput.print(true);
// de - allocate the PrintWriter object memory
pwInput.close();
// inform user that the file has been created
System.out.println("The data file has been created!");
}
}
Concerning the aforementioned program code, select the correct answer.
(1) The pWriter object is associated with the file named data.txt .
(a) True (b) False
(2) The println() method is used to write different data types to the file.
(a) True (b) False
(3) throws Exception is necessary to include in the program to prevent an unhandled exception type FileNotFoundException.
(a) True (b) False
(4) When the program is executed, only three lines of data are written to the text file.
(a) True (b) False
(5) The statement pwInput.close(); is executed before the data is written to the file.
(a) True (b) False
import java.io.File;
import java.io.PrintWriter;
public class TextFile {
public static void main(String[] args) throws Exception {
// associate a file with a file object
File fileExample = new File("data.txt");
// associate the file with a PrintWriter object
PrintWriter pwInput = new PrintWriter(fileExample);
// write string data to the file
pwInput.println("Sammy Student");
// write string data to the file
pwInput.println("Engineer");
// write a double - precision number to the file
pwInput.println(35.50);
// write a Boolean value to the file
pwInput.print(true);
// de - allocate the PrintWriter object memory
pwInput.close();
// inform user that the file has been created
System.out.println("The data file has been created!");
}
}
Explanation / Answer
1) The pWriter object is associated with the file named data.txt . -> TRUE
Explanation : TRUE, Because into the data.txt file data would be write so that's why the pWriter is associated with the data.txt file.
2) The println() method is used to write different data types to the file. -> TRUE
Explanation : TRUE, while the user enter the String, int,double,char, boolean etc. like this data types data would be enter into the file's by using the println()
3) throws Exception is necessary to include in the program to prevent an unhandled exception type FileNotFoundException -> TRUE
Explanation : TRUE, while user want's write or read data into the file. sometime user will not find the correct file details at this time user will get the FileNotFoundException so for handle that Exception we use either try catch blocks or throws Exception.
4) When the program is executed, only three lines of data are written to the text file. ->TRUE
Explanation : TRUE, Expect Boolean other data would be written into the file.
5) The statement pwInput.close(); is executed before the data is written to the file.
Explanation : TRUE, after the file succcessfully read or write would be completed then that close() will be performed.
Thanks...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.