I/O and Streams

The Java I/O Package
The Java I/O package, a.k.a. java.io, provides a set of input streams and a set of output streams used to read and write data to files or other input and output sources. There are three categories of classes in java.io: input streams, output streams and everything else.
The pages of this lesson provide overviews of Java's I/O classes. They give information about what each class does and how you can use them. These pages do not provide any practical examples or details of each class. For more practical information regarding reading and writing data using these classes, see Input and Output Streams
Input Streams
Input streams read data from an input source. An input source can be a file, a string, or memory--anything that can contain data. All input streams inherit from InputStream--an abstract class that defines the programming interface for all input streams.
The InputStream class defines a programming interface for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the number of bytes that are available for reading, and resetting the current position within the stream. An input stream is automatically opened when you create it. You can explicitly close a stream with the close() method, or let it be closed implicitly when the object is garbage collected.
Output Streams
Output streams write data to an output source. Similar to input sources, an output source can be anything that can contain data: a file, a string, or memory.
The OutputStream class is a sibling to InputStream and is used to write data that can then be read by an input stream. The OutputStream class defines a programming interface for writing bytes or arrays of bytes to the stream and flushiing the stream. Like an input stream, an output stream is automatically opened when you create it. You can explicitly close an output stream with the close() method, or let it be closed implicitly when the object is garbage collected.
Everything Else
File
Represents a file on the host system.
RandomAccessFile
Represents a random access file.
StreamTokenizer
Tokenizes the contents of a stream.
The java.io package

The exception classes in the java.io package

The Java Utility Package
The final Java package, java.util, contains a collection of utility classes.
Data Structure Classes
A variety of useful classes implementing standard computer science data structures: including BitSet, Dictionary, Hashtable, Stack and Vector. The java.util package also defines the Enumeration interface which provides a protocol for classes to count through a set of values.
Date
Use the Date class to create and manipulate calendar dates in a system-independent fashion.
StringTokenizer
This class converts a String of text into its tokens.
Properties
This class implements persistent properties. The properties table contains key/value pairs where both the key and the value are Strings. This class is used by the System class to implement System properties.
Observer and Observable
Classes that implement the Observer interface can "watch" Observable objects for state changes. When an Observable object changes it notifies all of its Observers of the change.
Random-Number Generator
The Random class implements a random-number generator.
Enumeration
The Enumeration interface defines a generic programming interface for iterating through a set of values.
File and Stream I/O
File and stream I/O (input/output) refers to the transfer of data either to or from a storage medium. In the .NET Framework, the System.IO namespaces contain types that enable reading and writing, both synchronously and asynchronously, on data streams and files. These namespaces also contain types that perform compression and decompression on files, and types that enable communication through pipes and serial ports.
A file is an ordered and named collection of bytes that has persistent storage. When you work with files, you work with directory paths, disk storage, and file and directory names. In contrast, a stream is a sequence of bytes that you can use to read from and write to a backing store, which can be one of several storage mediums (for example, disks or memory). Just as there are several backing stores other than disks, there are several kinds of streams other than file streams, such as network, memory, and pipe streams.
Files and Directories
You can use the types in the System.IO namespace to interact with files and directories. For example, you can get and set properties for files and directories, and retrieve collections of files and directories based on search criteria.
Here are some commonly used file and directory classes:
* File - provides static methods for creating, copying, deleting, moving, and opening files, and helps create a FileStream object.
* FileInfo - provides instance methods for creating, copying, deleting, moving, and opening files, and helps create a FileStream object.
* Directory - provides static methods for creating, moving, and enumerating through directories and subdirectories.
* DirectoryInfo - provides instance methods for creating, moving, and enumerating through directories and subdirectories.
* Path - provides methods and properties for processing directory strings in a cross-platform manner.
In addition to using these classes, Visual Basic users can use the methods and properties provided by the Microsoft.VisualBasic.File IO.FileSystem class for file I/O.
IO Purposes and Features
The Java IO classes, which mostly consists of streams and readers / writers, are addressing various purposes. That is why there are so many different classes. The purposes addressed are summarized below:
* File Access
* Network Access
* Internal Memory Buffer Access
* Inter-Thread Communication (Pipes)
* Buffering
* Filtering
* Parsing
* Reading and Writing Text (Readers / Writers)
* Reading and Writing Primitive Data (long, int etc.)
* Reading and Writing Objects
These purposes are nice to know about when reading through the Java IO classes. They make it somewhat easier to understand what the classes are targeting.


Free Web Hosting