go to previous page   go to home page   go to next page

Answer:

No, it only reminds the user of the correct order. The user is expected to start the program without arguments to get a reminder.


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;
    }

    try
    {
      instr = 
        new DataInputStream(
          new BufferedInputStream(
            new FileInputStream( args[0] )));
      outstr = 
        new DataOutputStream(
          new BufferedOutputStream(
            new FileOutputStream( args[2] )));

      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("I/O Problems" );
    }

  }
}

The class File can be used to check if a file already exists. The program could be improved by using that class. This will be discussed in the next chapter.

A computer's operating system comes with a file copy command, so there is no practical use for this program. But it is a foundation for many programs that are of practical use. Various data transformations can be done by slipping in some statements between the reading and the writing of the byte.


QUESTION 24:

(Thought question: ) What modification will change this program so that it counts the number of bytes in a file?