Improved File Copy Program
The exists() method can test that a file
a program is about to create does not already exist.
Doing this in the file copying program prevents
accidental deletion of files.
Here is a start on the improved 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 (2) that the source file does exist.
|