Constructing a File Object does not Create a File!

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes.

Constructing a File Object
does not Create a File!

When a File object is constructed, no check is made to see if the pathName corresponds to an existing file or directory. If a file or directory of pathName does not exist, constructing a File object will not create it.

Here is an example program that constructs a File object and uses one of its methods. The constructor argument is a simple file name (which is also a relative path name).

import java.io.*;
class TestExist
{

  public static void main ( String[] args ) 
  {
    String pathName = "notLikely.txt" ;

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

}

Since pathName is a simple file name the File object will use the current directory. If you start the program from a command prompt, the current directory is the directory the command prompt is "in". This is the directory that is listed with a DIR command.

QUESTION 4:

What will the program probably print on the monitor?