Notes
Outline
Input & Output
The java.io package provides an extensive library of classes for input/output
Include the statement
import java.io.*;
Input and output contains all the classes used in file processing
Java provides STREAMS as a general mechanism for I/O
Streams implement sequential access of data
WHERE TO GET MORE INFO
documentation
JDK notes on the IO package and
the Sun tutorial
Wu’s textbook website at http://www.mhhe.com/engcs/compsci/wu2/
Official course website at http://www.cs.rit.edu/~cs2/
Examples of file I/O at File I/O Examples
Java tutorial at http://java.sun.com/docs/books/tutorial/essential/io/index.html
Dr Bayliss notes at 3-2 and 3-3
Streams
The most basic of input/output with Java is done with stream
The stream can flow from the program to the screen, from a keyboard to the program, to/from a disk file or other storage media, to a printer, or even to a network or the Web
The stream commands enable you to write to a disk file or to the output screen
Input & Output streams
Two kinds of streams
Byte streams
Character streams
Input Stream
An object that an application can use to read a sequence of data
Output stream
An object that an application can use to write a sequence of data
Input & Output streams Media
An array of bytes of characters
A file
A pipe
A network connection
Byte Streams
The abstract classes InputStream and OutputStream are the root of the inheritance hierarchies for handling  reading and writing bytes
Take a look at InputStream/OutputStrem documentation JDK notes on the IO package and the Sun tutorial
File Streams
FileInputStream
Byte input stream derived from InputStream
Let you read bytes from the connected file
FileOutputStream
Byte output stream derived from OutputStream
Let you write bytes to the connect file
Copy File Program - bytes
import java.io.*;
public class CopyBytes {
public static void main(String[]args)
       throws IOException {
File inputFile = new File(“one.tst");
File outputFile = new File(“two.tst“);
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
int byt;
 while ((byt = in.read()) != -1) {out.write(byt);    }
in.close();
out.close();
}
}
Pipes
A pipe == A uninterpreted stream of bytes used to communicate between:
Keyboard---SIS---Your program---SOS---Display
Input file-----FIS----Your program-----FOS----Output file
Copy File Program - buffered bytes
import java.io.*;
public class CopyBytesBuffered {
  public static void main(String[] args)throws IOException {
File inputFile = new File("miserable.wav");    File outputFile = new File("miserable1.wav");
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
    int c;
    while ((c = bin.read()) != -1) { bout.write(c);  }
    bin.close();
    bout.close(); }
}
Streams
FileInputStream and FileOutputStream provide the capability to read from and to write to disk files
Java has three standard IO objects: in, out, and err
Character Stream
Reader and Writer classes
Abstract classes
Roots of the inheritance hierarchies for streams that read and write characters
Take a look at Reader/Writer class doc
Character Stream
OutputStreamWriter
Subclass of Writer class for reading characters
InputStreamReader
Subclass of Reader class for writing characters
PrintWriter
Subclass of writer for writing textual representation of Java primitive values or objects
Copy File Program - chars
import java.io.*;
public class CopyChars {
  public static void main(String[] args)
                            throws IOException {
    File inputFile = new File(“one.tst");
    File outputFile = new File(“two.tst");
    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;
     while ((c = in.read()) != -1) {
      out.write(c);
    }
    in.close();
    out.close();
  }
}
Character Stream
Buffered Writer: BufferedWriter
For improving the efficiency of I/O
Buffered Reader: BufferedReader
To read text lines from a file
Subclass of Writer class for reading characters
Terminal Input/output
System.in:
InputStream Object
Byte I/O
System.out
PrintWriter Object
System.err
PrintWriter Object