go to previous page   go to home page   go to next page

Answer:

The file notLikely.txt Does Not exist.

No Checking

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." );
  }

}

The File constructor does not throw IOExceptions. The path name is not checked to see if it exists. The example program (above) has been changed so that the file name comes from the command line.

The File constructor does throw a NullPointerException if the argument is null, but this is an unchecked exception and need not be caught

Try running it with a variety of arguments on the command line, both files and directories. Some examples follow. Some of these arguments are relative path names, others are absolute pathnames, and some are directory names.



C:\chap87\programs>java  TestExist TestExist.java
The file TestExist.java exists.

C:\chap87\programs>java  TestExist ..\programs\TestExist.java
The file ..\programs\TestExist.java exists.

C:\chap87\programs>java  TestExist ..\programs
The file ..\programs exists.

C:\chap87\programs>java  TestExist C:\cai\cs151
The file C:\cai\cs151 exists.

C:\chap87\programs>java  TestExist C:\glarch.txt
The file C:\glarch.txt Does Not exist.

QUESTION 5:

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