The file notLikely.txt Does Not exist.
The constructor does not throw IOException
s.
No checking is done of the path name.
Here is the example program, slighlty improved:
import java.io.*;
class TestExist
{
public static void main ( String[] args )
{
String pathName;
if ( args.length == 1 )
pathName = args[0];
else
pathName = "";
File test = new File( pathName );
if ( test.exists() )
System.out.println( "The file " + pathName + " exists." );
else
System.out.println( "The file " + pathName + " Does Not exist." );
}
}
Try running it with a variety of arguments on the command line, both files and directories. Here are some examples:
C:\cai\cs151\Notes\chap87\programs>java TestExist TestExist.java The file TestExist.java exists. C:\cai\cs151\Notes\chap87\programs>java TestExist ..\programs\TestExist.java The file ..\programs\TestExist.java exists. C:\cai\cs151\Notes\chap87\programs>java TestExist ..\programs The file ..\programs exists. C:\cai\cs151\Notes\chap87\programs>java TestExist C:\cai\cs151 The file C:\cai\cs151 exists. C:\cai\cs151\Notes\chap87\programs>java TestExist C:\glarch.txt The file C:\glarch.txt Does Not exist.
Some of these arguments are relative path names, others are absolute pathnames. Some are directory names.
Is "C:\cai\cs151"
a relative or an absolute path name?