I/O Foundations and Classes
Streams
- 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
other).
- 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
java.io
-
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.
-
InputStream is the base class for byte stream input
classes
-
OutputStream is the base class for byte stream output
classes
-
Reader is the base class for character stream input
classes
-
Writer is the base class for character stream output
classes
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).
-
JFileChooser -- a Swing component for popping up a dialog box
for easy browsing and selection of a file on a local file system
Example:
CopyFileUsingByteStream.java -- older example, which uses
the file stream classes to copy one file to another
Buffered Streams
Examples
These examples work in earlier versions of Java. These use the
BufferedReader and BufferedWriter classes.
- Test1.java -- This program reads
an input file and writes an all-uppercase copy of it to an output
file. First argument is the input file, second argument is the
output file.
- Test2.java -- This program reads
an input file and counts the number of times a word appears. First
argument is the input file, second argument is the word to search
for.
- Test3.java -- This program uses
some classes from the java.util package in addition to a file
reader, and it lists all the words in the input file along with the number
of times each word appears. The one command-line argument is the
input file.
- Frost.txt -- here is a sample text
file you can use to test each of these programs
Newer Java capabilities (useful for File I/O)
With Java 1.5, a number of new classes were introduced that made I/O
tasks a little easier.
-
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
Input and Output of Objects
-- Some of these examples use
Scanner, Formatter, newer features