Title: I/O Fundamental Classes
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Input Output Fundamental Classes

1. Universal I/O classes - We are going to focus on I/O to a file. However, java makes all I/O look similar. So the same techniques for sending to a file, and reading a file apply to other I/O sources such as sending and receiving data from a browser.
2. byte streams Versus char streams - Java has 2 families of classes. One family of classes focuses on data flow that consists of bytes (8 bit quantities), and the other family of classes focuses on Text related data which in Java means Unicode char (16 bit quantities). In many cases it looks like you have 2 classes that do the same thing, but as you look at it closer you will see that the char streams focus on text related issues, and the byte related streams focus on raw data issues.
3. xxxReader, xxxWriter, xxxOutputStream, and xxxInputStream - The classes we will be discussing are either input related or output related. Classes that end with Stream are dealing with bytes. Classes that end with either Reader or Writer are dealing with Unicode chars.
4. Low Level I/O - At the lowest level, Java defines some Abstract classes that have read, write routines. Note that because we have abstract classes, here you can't just call the constructors to create an instance. You need some other class --- see the next section: Disk I/O
  - InputStream - Contains read routines for bytes
  - OutputStream - Contains write routines for bytes
  - Reader - Contains read routines for chars
  - Writer - Contains write routines for chars

5. Disk I/O - We are going to focus on Disk Input Output for now. To create instances of Low Level I/O for Disk I/O we can use one of the following classes.


    // Open an instance of Reader from a File
    String filename = "myfile.txt";

    //FileReader is derived from Reader
    FileReader f = new FileReader(filename);
    // or you can do the following

    //FileInputStream is derived from InputStream
    FileInputStream f2 = new FileInputStream(filename);

    // For output, you do the following:
    // The file will be created if it doesn't exist.  If it does
    // exit then it will be completely overwritten from the beginning.
    FileWriter f3 = new FileWriter(filename);

    // Open in append Mode
    FileWriter f4 = new FileWriter(filename, true);

    // OutputStream File writeable opens:
    FileOutputStream f5 = new FileOutputStream(filename);

    // Or in append mode
    FileOutputStream f6 = new FileOutputStream(filename, true);


6. Exceptions - Most of the I/O classes throw exceptions that must be dealt with. We are going to look at Exceptions in more detail in a later section. For now to get around exceptions we will need to keep the compiler happy by adding the phrase:
throws IOException at the end of the procedure containing the I/O routine. For example:
int MyRoutine throws IOException
{ ......
7. Sample LowLevel Copy routine:

import java.io.*;

public class Copy {
    static void byteCopy(String from, String to) throws IOException
    {
    // Using Non-buffered Low Level I/O byte stream operations

        int b;
        // Using byte stream  (8 bit)
        FileInputStream in = new FileInputStream(from);
        FileOutputStream out = new FileOutputStream(to);

        while ((b = in.read()) != -1)
           out.write(b);

        in.close();
        out.close();
    }

    static void charCopy(String from, String to) throws IOException
    {
    // Using Non-buffered Low Level I/O char stream operations

        int c;
        // Using Unicode Approach char stream  (16 bit)
        FileReader in = new FileReader(from);
        FileWriter out = new FileWriter(to);

        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        charCopy("Copy.java", "junk1.txt");
        byteCopy("junk1.txt", "junk2.txt");
    }
}




8. Buffering Classes - You can do all of your work with low level I/O routines, but your program will normally not be efficient. Reading or Writing the disk or almost any I/O device is inefficient without some kind of buffering. Reading the disk 1 byte at a time is very slow, but reading the disk in 4k hunks runs a lot faster. However, your application often wants the data in smaller hunks than 4k at a time. A Buffering class provides a layer of software that allows the application to access the I/O in small hunks, while the actual reads/writes to the I/O device is done in some larger buffer size.
  - BufferedInputStream - Buffering class for the InputStream class
  - BufferedOutputStream - Buffering class for the OutputStream class
  - BufferedReader - Buffering class for the Reader class
  - BufferedWriter - Buffering class for the Writer class
9. Adding buffering to your application - To add buffering to your application, you wrap your stream or your Reader class with a buffering class. As the following example shows, the code can look mostly the same, we are just adding buffering support. The buffering constructors take as a parameter an instance of the low level input source. For example:

import java.io.*;

public class CopyBuffered {
    static void byteCopy(String from, String to) throws IOException
    {
       int b;
    // Using Buffered I/O byte(8 bit) stream operations

        FileInputStream insrc = new FileInputStream(from);
        FileOutputStream outsrc = new FileOutputStream(to);
        BufferedInputStream in = new BufferedInputStream(insrc);
        BufferedOutputStream out = new BufferedOutputStream(outsrc);


        while ((b = in.read()) != -1)
           out.write(b);

        in.close();
        out.close();
    }

    static void charCopy(String from, String to) throws IOException
    {
     int c;

    // Using Buffered I/O char(16 bit Unicode) stream operations

        FileReader insrc = new FileReader(from);
        FileWriter outsrc = new FileWriter(to);
        BufferedReader in = new BufferedReader(insrc);
        BufferedWriter out= new BufferedWriter(outsrc);


        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        charCopy("CopyBuffered.java", "junk1.txt");
        byteCopy("junk1.txt", "junk2.txt");
    }
}




Last Updated: July 28, 2013 11:27 PM