next up previous
Next: A program that (sometimes) Up: Unit 09 Previous: Output streams

Reading from an input stream and writing to an output stream

The operations for reading from input streams and writing to output streams are standardized, i.e., they do not depend on the particular stream being used. This means that we can use the same Java statements to read strings from a keyboard, from a file, or from the Internet. Such operations make use of the classes shown in the following diagram.

Example 1: reading from an input stream

InputStream is = ...; // keyboard, file or Internet
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();

Example 2: writing to an output stream (1)

OutputStream os = ...; // video or file
PrintWriter pw = new PrintWriter(os);
pw.println("Hello");

Example 3: writing to an output stream (2)

OutputStream os = ...; // video or file
PrintStream ps = new PrintStream(os);
ps.println("Hello");


next up previous
Next: A program that (sometimes) Up: Unit 09 Previous: Output streams