Skill-Building Exercises


Summary

The classes in the java.io package can be used to perform I/O operations on various types of data sinks and sources. A sink is a destination to which data is written, and a source is an origination point from which data is read. Strings, arrays, processes, and files are examples of the types of sinks and sources supported by the java.io classes. This chapter focused on using the java.io classes to perform I/O operations on files.

The java.io package can be intimidating to novice Java programmers. But there are several ways to organize the classes in the java.io package to help tame the conceptual complexity. A class hierarchy organization gives valuable clues regarding the intended use of the java.io classes.

It is also helpful to categorize java.io classes into file-terminal, intermediate, and user-fronting categories. File-terminal classes are used to create or open a file for input, output, or input/output operations. There are five file-terminal classes: FileInputStream, FileOutputStream, FileReader, FileWriter, and RandomAccessFile. Intermediate classes are used to enhance the performance of file terminal classes. Generally speaking, any class that contains the word Buffer is an intermediate class. User-fronting classes are those classes that have a wide variety of user interface methods that make byte, character, primitive type, and/or object I/O operations convenient for the programmer.

The File class represents the name of a file or directory and is used to manipulate file or directory metadata such as a file’s name, length, path, or the list of files in a directory, etc. The creation of a File object does not result in an actual file being created on disk, however, you can create an empty file by calling the createNewFile() method.

The InputStream and OutputStream classes operate on byte streams. With InputStreams and OutputStreams you can read and write bytes, primitive types, 8-bit characters, and serialized objects. When performing character I/O with byte streams the local default character encoding is utilized.

The Reader and Writer classes operate on 16-bit Unicode characters. The character encoding can be specified. This is an important internationalization feature.

The RandomAccessFile class is used to perform both file input and output on bytes, chars, and primitive types.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net