Using File Objects with Stream Constructors

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

  • What stream type is used with a byte-oriented input file?
    • FileInputStream
  • What stream type is used with a byte-oriented output file?
    • FileOutputStream

Using File Objects with Stream Constructors

Some constructors for IO streams that connect to disk files use a File object argument. (Previously, examples used constructors with String arguments). Here is a list of them:

FileInputStream(File file) throws IOException

FileOutputStream(File file) throws IOException

FileReader(File file) throws FileNotFoundException
 
FileWriter(File file) throws FileNotFoundException

Here is some more of the file copy program, now with blanks to fill in the constructors:

import java.io.*;
class CopyBytes
{
  public static void main ( String[] args ) 
  {
    DataInputStream  instr;
    DataOutputStream outstr;
    . . . .

    File inFile  = new File( args[0] );
    File outFile = new File( args[2] );

    . . . .

    try
    {
      instr = 
        new DataInputStream(
          new BufferedInputStream(
            new FileInputStream(  )));

      outstr = 
        new DataOutputStream(
          new BufferedOutputStream(
            new FileOutputStream(   )));
    . . . .

  }
}

Cut and paste from the following phrases:

inFile
outFile

QUESTION 8:

Fill in the blanks.