Testing that a File Does Exist
Using the ! operator with the exists() method
tests that a file (or a directory) does exist.
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;
}
. . . .
}
}
The rest of the program could remain unchanged without any problem,
but now that the two File objects have been constructed
and tested it would be nice to continue using them.
|