Download Chapter 10: File I/O

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 10: File I/O
Multiple Choice
1)
2)
3)
4)
5)
6)
7)
8)
An ___________ allows data to flow into your program.
input stream
output stream
file name
all of the above
An ____________ allows data to flow from your program.
input stream
output stream
file name
all of the above
The output stream connected to the computer screen is:
System.exit
System.quit
System.in
System.out
In Java, when you open a text file you should account for a possible:
FileNotFoundException
FileFullException
FileNotReadyException
all of the above
There are two common classes used for reading from a text file. They are:
PrintWriter and BufferedReader
FileInputStream and Scanner
BufferedReader and Scanner
None of the above
The scanner class has a series of methods that checks to see if there is any more wellformed input of the appropriate type. These methods are called __________ methods:
nextToken
hasNext
getNext
testNext
All of the following are methods of the Scanner class except:
nextFloat()
next()
useDelimiter()
readLine()
The method _________ reads a single character from an input stream.
readLine()
skip()
read()
close()
9)
10)
11)
12)
13)
14)
15)
When the method readLine() tries to read beyond the end of a file, it returns the value of:
1
-1
null
none of the above
A __________ path name gives the path to a file, starting with the directory that the
program is in.
relative
descendant
full
complete
The stream that is automatically available to your Java code is:
System.out
System.in
System.err
all of the above
All of the following are methods of the File class except:
exists()
delete()
getDirectory()
getName()
The class ObjectOutputStream contains all of the following methods except:
writeInt()
writeChar()
writeDouble()
println()
The method __________ from the File class forces a physical write to the file of any data
that is buffered.
close()
flush()
writeUTF()
writeObject()
The class ObjectInputStream contains all of the following methods except:
readLine()
readChar()
readObject()
readInt()

True/False
16)
23)
24)
A stream is an object that allows for the flow of data between your program and some I/O
device or some file.
Every input file and every output file used by your program has only one name which is the
same name used by the operating system.
The FileNotFoundException is a descendant of the class IOException.
When your program is finished writing to a file, it should close the stream connected to that
file.
Only the classes provided for file output contain a method named close.
The methods of the scanner class do not behave the same when reading from a text file as
they do when used to read from the keyboard.
Using BufferedReader to read integers from a file requires the String input to be parsed to
an integer type.
A full path name gives a complete path name, starting from the directory the program is in.
The File class contains methods that allow you to check various properties of a file.

Short Answer/Essay
25)
26)
27)
28)
29)
Explain the differences between a text file, an ASCII file and a binary file.
Write a Java statement to create and open an output stream to a file named autos.txt.
Explain what happens when an output file is opened in Java.
Write a Java method that returns a String representing a file name entered by the user.
Use the output stream to the file autos.txt created above in number 2 to write the line
“Mercedes” to the file.
What happens when the method close is invoked on a stream?
Create try and catch block that opens a file named statistics.txt for output. Writes the
integers 24, 55, and 76 to the file, and then closes the file.
17)
18)
19)
20)
21)
22)
30)
31)
32)
33)
34)
Write a Java statement that creates an output stream to append data to a file named
autos.txt.
Write a Java statement to create an input stream to a file named autos.txt.
Write a complete Java program using a Scanner object that opens a file named autos.txt and
displays each line to the screen.