Terms:
How to (techniques for 1.0 and 1.1)
And new with (1.1)
Newest - with 1.5 java.util.Scanner
These streams supersede the byte streams for all textual I/O
Subs:
The following are used for serialization:
One of the early inventions of the UNIX operating system was the pipe. By unifying disparate ways of communicating into a single metaphor, UNIX paved the way for a whole series of related inventions, culminating in the abstraction known as streams. [this info from Java in 21 days]
A stream is a path of communication between the source of some information and its destination. (Streams can be chained...info flows from one to the
other to the other). Overview of IO streams (old but nice hierarchy diagram) and newer tutorial version ("No matter how they work internally, all streams present the same simple model to programs that use them: a stream is a sequence of data.")
A pipe is an uninterpreted stream of bytes that can be used for communicating between programs or for reading and writing to peripheral devices and files. (Consider the stream as being piped to (or from) a source)
This information, an uninterpreted stream of bytes, can come from any
"pipe source", the computer's memory, the Internet, etc. In fact,
the source and destination of the information are completely arbitrary
producers and consumers of bytes. Therein lies the power of the abstraction;
you don't need to know about the source of the information when reading
from a stream or the destination when writing to one.
General-purpose methods accept a stream argument to specify that source or
destination. Arbitrary processors (or filters) of data have two stream
arguments - read first, write to second. The processors have no clue
about the streams - stream could be to files, could be to sockets, ...
UNIX users and users familiar with command lines have used a type of I/O commonly known as standard I/O.
Java gives access to standard I/O via the system class. Specifically, the three files are implemented in Instance Variables there.
implements stdin as an instanceof the InputStream class
(java.io.InputStream). The "standard" input stream. This stream
is already open and ready to supply input data. Typically this stream
corresponds to keyboard input or another input source specified by the
host environment or user.
read() allows you to read one byte of input.
implements Stdout as a PrintStream.
The "standard" output stream. This stream is already open and ready to
accept output data. Typically this stream corresponds to display output
or another output destination specified by the host environment or user.
implements stderr in the same was as stdout. The "standard" error output
stream. This stream is already open and ready to accept output data.
As with System.out, you have access to the PrintStream methods. By
convention, this output stream is used to display error messages or
other information that should come to the immediate attention of a user
even if the principal output stream, the value of the variable out, has
been redirected to a file or other destination that is typically not
continuously monitored.
Stdin
public static final InputStream in
With System.in, you have access to the read() and skip(long n) methods.
skip(long n) skips over n bytes of the input
Stdout
public static final PrintStream out
You can use the print() and println() methods supplying any
of the Java base types as arguments. For simple stand-alone Java
applications, a typical way to write a line of output data is:
System.out.println(data)
Stderr
public static final PrintStream err
Here is an example that works similarly to the UNIX cat or DOS type utility:
Common I/O classes - the tutorial and particularly: Character Streams and Byte Streams
Before we can perform I/O on a file, we need to have it!
The File class provides several utilities for dealing with files and obtaining basic information about these files.
Java In a Nutshell Files and Directories
To create a file, you can use any of three constructors:
The constructor you use will depend on the other file objects you need to access. For example, if you use only one file in your application, the first is easiest. Several files from a common directory indicates the other two.
See API for methods (e.g. canRead(), exists(), list(), length(), isFile(), isDirectory(), ...)
A sample stand-alone application to display basic file information on files passed in as command-line arguments:
import java.io.*;
/*
* fileInfo
* A simple File object test program.
*/
class fileInfo {
public static void main(String args[ ]) throws IOException {
File fileToCheck;
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
fileToCheck = new File(args[i]);
info(fileToCheck);
}
}
else {
System.out.println("No file given.");
}
}
public static void info (File f) throws IOException {
System.out.println("Name: " + f.getName( ));
System.out.println("Path: " + f.getPath( ));
if (f.exists()) {
System.out.print("File exists");
System.out.print((f.canRead() ? " and is Readable":""));
System.out.print((f.canWrite()? " and is Writable":""));
System.out.println(".");
System.out.println("File is "+f.length( )+" bytes.");
}
else {
System.out.println("File does not exist.");
}
}
}
InputStream and OutputStream are abstract classes that define methods for reading and writing bytes. Their subclasses allow bytes to be read from and written to a variety of sources and sinks.
FileInputStream and FileOutputStream read from and write to files. ByteArrayInputStream and ByteArrayOutputStream read from and write to an array of bytes in memory. PipedInputStream reads bytes from a PipedOutputStream (and vice versa with writes) These classes work together to implement a "pipe" for communication between threads.
Java In a Nutshell: Input and Output Streams interesting ... java.util.zip
FileInputStreams typically represent text files accessed in sequential order, byte by byte. With FileInputStreams, you can choose to access one byte, several bytes or the entire file.
To open, you give a String or a File object to the constructor
Reading a FileInputStream
If the security settings allow for retrieval of files, you can display the contents in a TextArea object.
FilterInputStream and FilterOutputStream are special - they filter input and output bytes. When a FilterInputStream is created, an InputStream is specified for it to filter. FilterInputStream and FilterOutputStream do not perform any
filtering themselves; this is done by their subclasses. Consider one of its subclasses: DataInputStream
DataInputStreams behave much like FileInputStreams. Data streams can directly read any of the native types (int, char, etc). Generally you use a DataInputStream with binary data files.
For opening an closing, use the same methods you do for FileInputStreams
When accessing a file as a DataInput Stream, you can use the same methods (eg. read() ) available to FileInputStream objects, but you also have access to methods designed specifically to read various data types
Each method will read one object of the type requested.
To read a short, for example
For the String readLine() method, you signal termination of the string with any of \n, \r, \r\n, or EOF.
Not any more....deprecated: the most noticable difference from version 1.0 is that the method readLine() from DataInputStream is no longer there.
It is now available in BufferedReader - Readers are for reading characters rather than bytes. More about this later.
Java provides you with the ability to use URLs as a means of accessing objects across a network.
We implicitly use a URL object when we access sounds and images using the
getDocumentBase() method for applets
One can open an input stream off of an appropriate URL. For example, you can include a data file for one of your applets:
Now you can use istream to read information like you do with a FileInputStream object
URL is at java.net.URL
Also:
You can also explicitly connect to the URL (so you could test - openConnection throws an IOException - as do most I/O streams).
openConnection() returns a URLConnection object that represents a connection to the remote object referred to by this URL.
Always remember that users may have their browsers protections set to not allow applets to access files.
Also, always remember to do I/O in try/catch blocks since
most I/O methods throw exceptions.
InputStream, FilterInputStream, OutputStream, and FilterOutpuStream are all abstract classes (in API).
They provide the methods for the specific classes used (BufferedInputStream, DataInputStream, LineNumberInputStream, PushbackInputStream, BufferedOutputStream, DataOutputStream, PrintStream)
(see page 383 "Java in 21 Days")
Java version 1.1 has some changes in the I/O. Here is more info.
Most noticable are the following:
Reader is the superclass of all character input streams and
Subs:
The following are used for serialization:
the most noticable difference from version 1.0 is that the
method readLine() from DataInputStream is no longer there.
It is now available in BufferedReader
java.io.Reader
methods:
SubClasses:FileInputStreams
or
When finished with a file, close it explicitly (or will close with garbage collect)
Displaying a File (also FileViewer and FileLister in Java in a Nutshell 1) (old code)
FilterInputStream
see hierarchy
However, you can use a direct URL if you want:
openStream() is a method in URL that opens a connection to this URL and returns an InputStream for reading from that connection.
I/O Streams: appended 1.1
the IO API again
* Reader
* Writer
Writer is the superclass of all character output streams.
These streams supersede the byte streams for all textual I/O
File (Reader/Writer)
* ObjectStreamClass (to represent a class being serialized)
* ObjectOutputStream (to serialize to a stream)
* ObjectInputStream (to deserialize from a stream)
Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined in order to provide higher efficiency, additional functionality or both.
close()
mark(int) : Mark the present location
read() : Read a single character
read(char[]) : Read characters into an array
read(char[], int, int) : Read characters into a portion of an array
ready()
reset()
skip(long) : Skip characters
BufferedReader
CharArrayReader
FilterReader:abstract class for reading filtered character streams
InputStreamReader - FileReader
PipedReader
StringReader : A character stream whose source is a string