Java2Module1
Which of the following is the legal mode for creating a new RandomAccessFile stream?
"rw"
What are the reasons to create an instance of the File class?
- to determine whether the file exists -to obtain the properties of the file such as whether the file can be read, written, or is hidden - to rename the file - to delete the file
After the following program is finished, how many bytes are written to the file t.dat? import java.io.*; public class Test{ public static void main(String[]args) throws IOException{ DataOutputStream output = newDataOutputStream( newFileOutputStream("t.dat")); output.writeChar('A'); output.close(); } }
2 bytes
After the following program is finished, how many bytes are written to the file t.dat? import java.io.*; public class Test{ public static void main(String[]args) throws IOException{ DataOutputStream output = newDataOutputStream( newFileOutputStream("t.dat")); output.writeShort(1234); output.writeShort(5678); output.close(); } }
4 bytes
To create a file, you can use ________.
All of these
Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?
DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat"));
Which class contains the method for checking whether a file exists?
File
Which of the following returns the path separator character?
File.pathSeparatorChar
Which type of exception occurs when creating a DataInputStream for a nonexistent file?
FileNotFoundException
What does the following code do? FileInputStream fis = new FileInputStream("test.dat");
It creates a FileInputStream for test.dat if test.dat exists.
Which class do you use to write data into a text file?
PrintWriter
With which I/O class can you append or update a file?
RandomAccessFile(),
Which class do you use to read data from a text file?
Scanner
Which of the following statements is not true?
The methods in an object are serialized.
A compilation error will occur if the file doesn't exist when creating a FileInputStream for the file.
false
The FileFoundException occurs when creating FileOutputStream for a nonexistent file.
false
Which of the following statements creates an instance of File on Window for the file c:\temp.txt?
new File("c:\\temp.txt")
To append data to an existing file, use ________ to construct a FileOutputStream for file out.dat.
new FileOutputStream("out.dat", true)
Which method can be used to create an output object for file temp.txt?
new PrintWriter("temp.txt") new PrintWriter(new File("temp.txt"))
Which method can be used to create an input object for file temp.txt?
new Scanner(new File("temp.txt"))
Which method can be used to read a whole line from the file?
nextLine
To create an InputStream to read from a file on a Web server, you use the method ________ in the URL class.
openStream();
InputStream and OutputStream are abstract classes.
true
RandomAccessFile, DataInputStream, and DataOutputStream share the same file formats.
true
The FileNotFoundException occurs when creating FileInputStream for a nonexistent file.
true
The FileWriter, FileReader, FileInputStream, FileOutputStream, and RandomAccessFile classes can be used to process external files.
true
The read method, the write method, and creating stream constructors can throw IOException errors.
true
To determine whether a file exists, you can use the exists() method on a File object.
true