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.

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

This book will show you that not only can <strong>Java</strong> handle these two tasks with relative ease and<br />

grace; it can do anything C and C++ can do, and a whole lot more. <strong>Java</strong>'s I/O capabilities not<br />

only match those of classic languages like C and Pascal, they vastly surpass them.<br />

The most common complaint about <strong>Java</strong> I/O among students, teachers, authors of textbooks,<br />

and posters to comp.lang.java is that there's no simple way to read a number from the console<br />

(System.in). Many otherwise excellent introductory <strong>Java</strong> books repeat this canard. Some<br />

textbooks go to great lengths to reproduce the behavior they're accustomed to from C or<br />

Pascal, apparently so teachers don't have to significantly rewrite the tired Pascal exercises<br />

they've been using for the last 20 years. However, new books that aren't committed to the old<br />

ways of doing things generally use command-line arguments for basic exercises, then rapidly<br />

introduce the graphical user interfaces any real program is going to use anyway. Apple wisely<br />

abandoned the command-line interface back in 1984, and the rest of the world is slowly<br />

catching up. [1] Although System.in and System.out are certainly convenient for teaching and<br />

debugging, in 1999 no completed, cross-platform program should even assume the existence<br />

of a console for either input or output.<br />

The second common complaint about <strong>Java</strong> I/O is that it can't handle formatted output; that is,<br />

that there's no equivalent of printf() in <strong>Java</strong>. In a very narrow sense, this is true because<br />

<strong>Java</strong> does not support the variable length argument lists a function like printf() requires.<br />

Nonetheless, a number of misguided souls (your author not least among them) have at one<br />

time or another embarked on futile efforts to reproduce printf() in <strong>Java</strong>. This may have<br />

been necessary in <strong>Java</strong> 1.0, but as of <strong>Java</strong> 1.1, it's no longer needed. The java.text package,<br />

discussed in Chapter 16, provides complete support for formatting numbers. Furthermore, the<br />

java.text package goes way beyond the limited capabilities of printf(). It supports not<br />

only different precisions and widths, but also internationalization, currency formats,<br />

percentages, grouping symbols, and a lot more. It can easily be extended to handle Roman<br />

numerals, scientific or exponential notation, or any other number format you may require.<br />

The underlying flaw in most people's analysis of <strong>Java</strong> I/O is that they've confused input and<br />

output with the formatting and interpreting of data. <strong>Java</strong> is the first major language to cleanly<br />

separate the classes that read and write bytes (primarily, various kinds of input streams and<br />

output streams) from the classes that interpret this data. You often need to format strings<br />

without necessarily writing them on the console. You may also need to write large chunks of<br />

data without worrying about what they represent. Traditional languages that connect<br />

formatting and interpretation to I/O and hard-wire a few specific formats are extremely<br />

difficult to extend to other formats. In essence, you have to give up and start from scratch<br />

every time you want to process a new format.<br />

Furthermore, C's printf(), fprintf(), and sprintf() family only really works well on<br />

Unix (where, not coincidentally, C was invented). On other platforms, the underlying<br />

assumption that every target may be treated as a file fails, and these standard library functions<br />

must be replaced by other functions from the host API.<br />

<strong>Java</strong>'s clean separation between formatting and I/O allows you to create new formatting<br />

classes without throwing away the I/O classes, and to write new I/O classes while still using<br />

the old formatting classes. Formatting and interpreting strings are fundamentally different<br />

1 MacOS X will reportedly add a real command-line shell to the Mac for the first time ever. Mainly, this is because MacOS X has Unix at its heart.<br />

However, Apple at least has the good taste to hide the shell so it won't confuse end users and tempt developers away from the righteous path of<br />

graphical user interfaces.<br />

2

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

Saved successfully!

Ooh no, something went wrong!