Prev | Current Page 231 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

If the user provided exactly one argument, the program assumes this
argument is the name of a file, and it attempts to open the file using a BufferedReader inside a try block.
If all goes well, the program enters a while loop calling the readLine() method of the
BufferedReader to read a line of the file at a time into the String variable line, and then display the
text using the println() method. The loop will terminate when readLine() returns a null value; that is
the signal that the BufferedReader has encountered the end-of-file (EOF).
The program uses the finally block to close the file. Since the close() method can itself
throw an IOException, the call to close() within the finally block must also be surrounded by
a try block. If an error occurs here, however, this program simply ignores it; the catch block is empty
of any code.
Our second example reads a file for the simple purpose of copying the bits in the file to a new file. For this
purpose we use a BufferedInputStream wrapped around a FileInputStream. The program copies
the bits literally, one byte at a time. This approach is highly efficient because there is no overhead of translating
the bit patterns into characters.
import java.io.*;
/**
* Copy files from the command line.


Pages:
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
oferty spa Wczasy nad morzem projektowanie wnętrz opony michelin wyciągarki