I/O Foundations and Classes
- In Java, I/O handled with streams.
- Think of a stream as a sequence of data flowing from one place
to another (useful to think of as a buffer of data going one way or the
- An input stream flows from an input source (e.g. keyboard, file) into a program, usually to be stored in variables
- An output stream flows from a program to an output destination (e.g. screen, file, network socket)
- Three stream objects already associated with devices in Java:
- System.in -- standard input stream object
- System.out -- standard output stream object
- System.err -- standard error stream object
- I/O libraries are in the package
- Package Tree -- You can see the hierarchy of java.io classes here
- Many built-in stream classes in Java, for processing different types of data. Two primary categories: byte streams and character streams.
Reading and Writing with Files
File is a class used for retrieving properties of files or
directories on a disk.
- Objects of class File do not open files or provide file processing features.
- Text files are typically created with character-based streams. Binary files are typically created with byte streams.
- Sequential File: no regular record structure, typically read or written as an entire file
- Random Access File: structured as uniformly-sized records. Can read/write either sequentially or by accessing single records anywhere in the file.
- Basic File I/O classes.
- For the input streams, the primary method is called read. There is a version that reads one byte (or char), and a version that reads an array of bytes (or chars).
- For the output streams, the primary methods are called write. For writing single bytes (or chars) and for arrays of byte (or chars).
- Buffered streams help increase efficiency. They use a buffered array of bytes or characters. The buffered stream classes are:
- Buffered streams are used to add functionality to other streams (i.e. the buffering). Usually constructed out of other stream objects
- For example, to applied buffered streams to file i/o, we can create
file streams, and then wrap them in a buffered stream object. Examples:
BufferedReader infile1 = new BufferedReader(new FileReader(filename)); BufferedOutputStream outfile1 = new BufferedOutputStream(new FileOutputStream(filename)); Reader r1 = new BufferedReader(new FileReader("file1.txt"));Note that in the last declaration, the buffered reader object is attached to a Reader reference variable. This is legal since Reader is the base class for all reader classes
- Formatter class -- an interpreter for printf-style format strings. Can create a Formatter object already attached to a file
- Scanner class -- a text scanner that parses simple tokens more easily. Can create a Scanner and pass in an input stream, or a File object
- Serialization of objects: representing an object as a sequence
of bytes, which includes:
- the object's data
- info about the object's type
- info about the types of data in the object
- A serialized object can be written to a file as a single item, and then it can be read from a file and deserialized
- This can be done with the classes:
- An object can be serialized if its class implements the interface
- Serializable is a marker (or tagging) interface
- To write an object with class ObjectOutputStream, it must be a Serializable object
- Each instance variable in the object must also be Serializable (or must be declared transient, and will be left out of the serialization)
- Can combine with File I/O streams. Example:
ObjectOutputStream output; output = new ObjectOutputStream(new FileOutputStream("myfile.dat"));