Java Training: Input/Output
Input/Output for Applets and Applications
There are security restrictions on direct file I/O in applets.
Applet can only establish a socket stream to the server
from which this applet was downloaded.
Application, from another side, can use all power of Java I/O streams.
I/O Streams in Java
Java I/O is mostly based on a set of stream classes.
Streams present input/output data as sequentional file-like objects.
Stream Classes
InputStream and OutputStream are two basic
stream classes, that permit reading/writing data as bytes.
Most of the I/O classes are derived from those two classes.
FileInputStream and FileOutputStream access disk files.
ByteArrayInputStream and ByteArrayOutputStream deal with
in-memory arrays of bytes.
PipedInputStream and PipedOutputStream connect to each other
enabling one part of a Java program to read output produced by another part.
Pipe streams should be used with care to avoid possible file deadlock.
Reader and Writer classes
BufferedReader reads text from a character-input stream,
buffering characters so as to provide for the efficient
reading of characters, arrays, and lines.
It is advisable to wrap a BufferedReader around any Reader whose read()
operations may be costly, such as FileReaders and InputStreamReaders.
For example,
BufferedReader in
= new BufferedReader(new FileReader("foo.in"));
BufferedWriter writes text to a character-output stream,
providing efficient bufferring.
A newLine() method is provided,
which uses the platform's own notion of line separator.
It is advisable to wrap a BufferedWriter around any Writer whose write()
operations may be costly, such as FileWriters and OutputStreamWriters.
For example,
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));