Complete Program

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:

The complete program, including the filled-in blanks, is below:

Complete Program

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

    if ( args.length != 3 || !args[1].toUpperCase().equals("TO") )
    {
      System.out.println("java CopyBytes source to destination");
      return;
    }

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

    if ( outFile.exists() )
    {
      System.out.println( args[2] + " already exists");
      return;
    }

    if ( !inFile.exists() )
    {
      System.out.println( args[0] + " does not exist");
      return;
    }

    try
    {
      instr = 
        new DataInputStream(
          new BufferedInputStream(
            new FileInputStream( inFile )));
      outstr = 
        new DataOutputStream(
          new BufferedOutputStream(
            new FileOutputStream( outFile  )));

      try
      {
        int data;
        while ( true )
        {
          data = instr.readUnsignedByte() ;
          outstr.writeByte( data ) ;
        }
      }
      catch ( EOFException  eof )
      {
        outstr.close();
        instr.close();
        return;
      }
    }

    catch ( FileNotFoundException nfx )
    {
      System.out.println("Problem opening files" );
    }
    catch ( IOException iox )
    {
      System.out.println("IO Problems" );
    }

  }
}

QUESTION 9:

(Thought question: ) Can this program copy a file from one directory to another?