Title: PrintStream Scanner Constructors
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

PrintStream Scanner Constructors

If you look at the constructors for PrintStream, Scanner you will see multiple ways to construct these classes.

In these notes we want to investigate some of the options. Note that we will encounter some new classes:

- File - This class represents something more like a pathname. You can construct a File class without the real file actually existing on your disk. We will discuss the File class more in the course.
- FileInputStream - This is a low level input class for reading bytes from a disk file. FileInputStream extends InputStream which is an abstract class that defines input at a very low level in Java. Note that System.in is an instance of InputStream.
- FileOutputStream - This is a low level input class for writing bytes to a disk file. FileOutputStream extends OutputStream which is an abstract class that defines output at a very low level in Java.

To demonstrate some of these options, we have the following example:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;


public class Constructors_Printstream_Scanner {
    void copyByLine(Scanner scanner, PrintStream printStream)
    {
        while (scanner.hasNextLine())
        {
            String s= scanner.nextLine();
            printStream.println(s);
        }
    }

    void approach1(String inputFileName, String outputFileName)
    {
        Scanner scanner=null;
        PrintStream printStream=null;

        try
        {
            FileInputStream fi = new FileInputStream(inputFileName);
            scanner = new Scanner(fi);

            FileOutputStream fo = new FileOutputStream(outputFileName);

            //If you want to append to a file, you can use this constructor instead
            //FileOutputStream fo = new FileOutputStream(outputFileName, true);

            printStream = new PrintStream(fo);
            copyByLine(scanner, printStream);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("error: "+e);
        }
        finally
        {
            if (scanner != null)
                scanner.close(); // note this closes the FileInputStream
            if (printStream != null)
                printStream.close(); // note this closes the FileOutputStream
        }
    }

    void approach2(String inputFileName, String outputFileName)
    {
        Scanner scanner=null;
        PrintStream printStream=null;

        try
        {

// The File class is kind of like a pathname.
//You can construct a File class even if the object doesn't really exist

            File fi = new File(inputFileName);
            scanner = new Scanner(fi);
            File fo = new File(outputFileName);
            printStream = new PrintStream(fo);
            copyByLine(scanner, printStream);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("error: "+e);
        }
        finally
        {
            if (scanner != null)
                scanner.close();
            if (printStream != null)
                printStream.close();
        }
    }


    void approach3(String inputFileName, String outputFileName)
    {
        Scanner scanner=null;
        PrintStream printStream=null;

        try
        {
            // Big Trouble .... The following compiles, but doesn't do what we want ... WHY?
            scanner = new Scanner(inputFileName);
            // What we really want is:
            //scanner = new Scanner("first line \n second line \n third line\n ....");

            printStream = new PrintStream(outputFileName);
            copyByLine(scanner, printStream);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("error: "+e);
        }
        finally
        {
            if (scanner != null)
                scanner.close();
            if (printStream != null)
                printStream.close();
        }
    }

    public static void main(String[] args)
    {
        Constructors_Printstream_Scanner cps = new Constructors_Printstream_Scanner();
        cps.approach1(args[0], args[1]);
        //cps.approach2(args[0], args[1]);
        //cps.approach3(args[0], args[1]);
    }

}


You can use the following input file to read:


She sells sea shells down by
the sea shore.
Peter Piper picked
...


Last Updated: May 26, 2015 4:26 PM