Improved File Copy 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        

Is "C:/cai/cs151" a relative or an absolute path name?

Answer:

Absolute. (It is the absolute path name of a directory).

Improved File Copy Program

The exists() method tests if a file exists. Do this when a program is about to create a file, and you don't want to destroy a previous file of the same name. Here is a start on a 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(  );

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

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

    .  .  .  .  

  }
}

Now the program tests that: (1) the destination file does not already exist, and that (2) the source file does exist. Cut and paste the following phrases:

exists()
args[2]
!

(Some phrases may be used more than once.)

QUESTION 6:

Fill in the blanks.