The file notLikely.txt Does Not exist.
The constructor does not throw IOExceptions
.
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 name files, others name directories.