A Quick Look at Java’s Disk Access Facilities
Templates look very nice. But knowing how the templates work is even better. Here are a few tidbits describing the inner workings of Java’s disk access code:
PrintStreamis something you can use for writing output.
PrintStream is like a
Scanner. The big difference is that a
Scanner is for reading input and a
PrintStream is for writing output.
PrintStream is defined in the Java API.
- The expression
new File("rawData.txt")plays the same role that
System.inplays in many other programs.
System.in stands for the computer’s keyboard, the expression
new File("rawData.txt") stands for a file on your computer’s hard drive. When the computer calls
new File("rawData.txt"), the computer creates something like
System.in — something you can stuff inside the
new Scanner( ) parentheses.
File is defined in the Java API.
FileNotFoundExceptionis something that may go wrong during an attempt to read input from a disk file (or an attempt to write output to a disk file).
Disk file access is loaded with pitfalls. Even the best programs run into disk access trouble occasionally. To brace against such pitfalls, Java insists on your adding some extra words to your code.
The added words
FileNotFoundException form a throws clause. A throws clause is a kind of disclaimer. Putting a throws clause in your code is like saying, “I realize that this code can run into trouble.”
Of course, in the legal realm, you often have no choice about signing disclaimers. “If you don’t sign this disclaimer, the surgeon won’t operate on you.” Okay then, I’ll sign it. The same is true with a Java throws clause. If you put things like
new PrintStream("cookedData.txt") in your code and you don’t add something like throws
FileNotFoundException, the Java compiler refuses to compile your code.
So when do you need this
throws FileNotFoundException clause, and when should you do without it? Well, having certain things in your code — things like
new PrintStream("cookedData.txt") — forces you to create a throws clause. You can spend some time learning all about the kinds of things that demand throws clauses. But at this point, it’s better to concentrate on other programming issues.
FileNotFoundException is — you guessed it — defined in the Java API.
- A call to the
closemethodends the connection between your program and the file.
In many examples, you sever the connection between your program and the computer keyboard by calling
keyboard.close(). The same is true when you call the
close method for a disk file’s scanner or a disk file’s
PrintStream instance. Calling the
close method reminds Java to finish all pending read or write operations and to break the program’s connection to the disk file, the keyboard, or to whatever else holds data for the program.
In a serious, make-it-or-break-it application, the proper placement of
close calls is important. These
close calls ensure the proper completion of the program’s input and output actions and help free up disk resources for use by other running programs.