Java File I/O
To open a file for reading, you use the following classes. File and Writer File and Output File and Input File and Scanner
File and Scanner
Which of the following statements opens a file named MyFile.txt and allows you to read data from it? Scanner inputFile = new Scanner("MyFile.txt"); File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); File Scanner = new File("MyFile.txt"); PrintWriter inputFile = new PrintWriter("MyFile.txt");
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
This class allows you to read a line from a file. FileWriter Scanner InputFile FileReader
FileWriter
Which of the following statements opens a file named MyFile.txt and allows you to append data to its existing contents? FileWriter fwriter = new FileWriter("MyFile.txt"); PrintWriter outFile = new PrintWriter(fwriter); FileWriter fwriter = new FileWriter("MyFile.txt"", true); PrintWriter outFile = new PrintWriter(fwriter); PrintWriter outfile = new PrintWriter("MyFile.txt"", true); PrintWriter outfile = new PrintWriter(true
FileWriter fwriter = new FileWriter("MyFile.txt"", true); PrintWriter outFile = new PrintWriter(fwriter);
A random number, created as an object of the Random class, is always a(n) __________. object integer float class
Integer
This class allows you to use the print and println methods to write data to a file. File FileReader OutputFile PrintWriter
PrintWriter
To open a file for writing, you use the following class. PrintWriter FileOpen OutputFile FileReader
PrintWriter
Which of the following statements will create an object from the Random class? randomNumbers() = new Random(); Random myNumber = new Random(); myNumber = new Random(); Random = new randomNumbers();
Random myNumber = new Random();
True or False: A file must always be opened before using it and closed when the program is finished using it.
True
True or False: When you pass the name of a file to the PrintWriter constructor and the file already exists, it will be erased and a new empty file with the same name will be created.
True
When a program is finished using a file, it should do this. erase the file close the file throw an exception reset the read position
close the file
Given the following statement, which statement will write the string "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt"); System.out.println(diskOut PrintWriter.println("Calvin"); DiskFile.println("Calvin"); diskOut.println("Calvin");
diskOut.println("Calvin");
When working with the PrintWriter class, which of the following import statements should you have near the top of your program? import javax.swing.*; import javac.io.*; import java.io.*; import java.file.*;
import java.io.*;
Which method of the Random class will return a random number within the range of 0.0 and 1.0? nextDouble() nextShort() nextLong() nextInt(int n)
nextDouble()
Which of the following is the method you can use to determine whether a file exists? the File class's canOpen method the Scanner class's exists method the File class's exists method the PrintWriter class's fileExists method
the File class's exists method