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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13<br />

Chapter 14<br />

Chapter 15<br />

Chapter 16<br />

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

not only how to use the File class, but also the precautions you need to take to make<br />

your file code portable across all major platforms that support <strong>Java</strong>.<br />

Filenames are problematic, even if you don't have to worry about cross-platform<br />

idiosyncrasies. Users forget filenames, mistype them, can't remember the exact path to<br />

files they need, and more. The proper way to ask a user to choose a file is to show<br />

them a list of the files and let them pick one. Most graphical user interfaces provide<br />

standard graphical widgets for selecting a file. In <strong>Java</strong>, the platform's native file<br />

selector widget is exposed through the java.awt.FileDialog class. Like many<br />

native peer-based classes, however, FileDialog doesn't behave the same or provide<br />

the same services on all platforms. Therefore, the <strong>Java</strong> Foundation Classes 1.1<br />

(Swing) provide a pure <strong>Java</strong> implementation of a file dialog, the<br />

javax.swing.JFileChooser class. Chapter 13 shows you how to use both these<br />

classes to provide a GUI file selection interface. In the final example, you'll add a<br />

Swing-based GUI to the File Viewer program.<br />

We live on a planet where many languages are spoken, yet most programming<br />

languages still operate under the assumption that everything you need to say can be<br />

expressed in English. <strong>Java</strong> is starting to change that by adopting the multinational<br />

Unicode as its native character set. All <strong>Java</strong> chars and strings are given in Unicode.<br />

However, since there's also a lot of non-Unicode legacy text in the world, in a<br />

dizzying array of encodings, <strong>Java</strong> also provides the classes you need to read and write<br />

this text in these encodings as well. Chapter 14 introduces you to the multitude of<br />

character sets used around the world, and develops a simple applet to test which ones<br />

your browser/VM combination supports.<br />

A language that supports international text must separate the reading and writing of<br />

raw bytes from the reading and writing of characters, since in an international system<br />

they are no longer the same thing. Classes that read characters must be able to parse a<br />

variety of character encodings, not just ASCII, and translate them into the language's<br />

native character set. Classes that write characters must be able to translate the<br />

language's native character set into a variety of formats and write those. In <strong>Java</strong>, this<br />

task is performed by the Reader and Writer classes. Chapter 15 shows you how to<br />

use these classes, and adds support for multilingual text to the File Viewer program.<br />

<strong>Java</strong> 1.0 did not provide classes for specifying the width, precision, and alignment of<br />

numeric strings. <strong>Java</strong> 1.1 and later make these available as subclasses of<br />

java.text.NumberFormat. As well as handling the traditional formatting achieved by<br />

languages like C and Fortran, NumberFormat also internationalizes numbers with<br />

different character sets, thousands separators, decimal points, and digit characters.<br />

Chapter 16 shows you how to use this class and its subclasses for traditional tasks, like<br />

6

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

Saved successfully!

Ooh no, something went wrong!