23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

}<br />

}<br />

<strong>Java</strong> I/O<br />

Since we're no longer writing to System.out and reading from System.in, it's important to<br />

make sure the streams are closed when we're done. This is a good use for a finally clause, as<br />

we need to make sure the files are closed whether or not the reads and writes succeed.<br />

<strong>Java</strong> is better about closing files than most languages. As long as the VM doesn't terminate<br />

abnormally, the files will be closed when the program exits. Still, if this class is used inside a<br />

long-running program like a web server, waiting until the program exits isn't a good idea;<br />

other threads and processes may need access to the files.<br />

There is one bug in this program: it does not behave well if the input and output files are the<br />

same. While it would be straightforward to compare the two filenames before copying, this is<br />

not safe enough. Once aliases, shortcuts, symbolic links, and other factors are taken into<br />

account, a single file may have multiple names. The full solution to this problem will have to<br />

wait until Chapter 12, when we discuss canonical paths and temporary files.<br />

4.3 File Viewer, Part 1<br />

I often find it useful to be able to open an arbitrary file and interpret it in an arbitrary fashion.<br />

Most commonly I want to view a file as text, but occasionally it's useful to interpret it as<br />

hexadecimal integers, IEEE 754 floating-point data, or something else. In this book, I'm going<br />

to develop a program that lets you open any file and view its contents in a variety of different<br />

ways. In each chapter, I'll add a piece to the program until it's fully functional. Since this is<br />

only the beginning of the program, it's important to keep the code as general and adaptable as<br />

possible.<br />

Example 4.3 reads a series of filenames from the command line in the main() method. Each<br />

filename is passed to a method that opens the file. The file's data is read and printed on<br />

System.out. Exactly how the data is printed on System.out is determined by a commandline<br />

switch. If the user selects ASCII format (-a), then the data will be assumed to be ASCII<br />

(more properly, ISO Latin-1) text and printed as chars. If the user selects decimal dump (-d),<br />

then each byte should be printed as unsigned decimal numbers between and 255, 16 to a line.<br />

For example:<br />

000 234 127 034 234 234 000 000 000 002 004 070 000 234 127 098<br />

Leading zeros are used to maintain a constant width for the printed byte values and for each<br />

line. A simple selection algorithm is used to determine how many leading zeros to attach to<br />

each number. For hex dump format (-h), each byte should be printed as two hexadecimal<br />

digits. For example:<br />

CA FE BA BE 07 89 9A 65 45 65 43 6F F6 7F 8F EE E5 67 63 26 98 9E 9C<br />

Hexadecimal encoding is easier, because each byte is always exactly two hex digits. The<br />

static Integer.toHexString() method is used to convert each byte read into two<br />

hexadecimal digits.<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!