File Names and File Paths

Specifying which file to open involves not only the file name but also the extension and the path.

The name of the file is what Windows Explorer shows you in the right pane of Figure 1. The names of four files are listed: FilePaths.html, FilePaths.jpr, fName_1.txt, and Main.java.


Figure 1

The extensions indicate the kind of data contained in the file. Extensions are the part of the filename after the last period.

Finally, the path specifies how to find the file on the disk. Windows Explorer shows the path beside the word “Address”. All four files shown above have the same path: “D:\cs132\W03\L03\filePaths”. The “D:” specifies which of possibly several disk drives contains the file. Each of the names between slashes specifies a folder or directory. Each directory, except the first, is contained within the directory immediately to its left in the path. This is shown graphically in Windows Explorer's left pane.

Open the FilePaths.jpr file. When you navigate to it with Windows Explorer, it should appear as in Figure 1 except for the disk drive.

Absolute Paths

Main.java contains the following code:

	package filePaths;
	import becker.io.TextOutput;

	public class Main extends Object
	{
	   public static void main(String[] args)
	   {  String pathName = "U:\\cs132\\W03\\L03\\filePaths\\";
	      String fileName = "fName_1.txt";

	      System.out.println("pathName = '" + pathName + "'.");
	      System.out.println("fileName = '" + fileName + "'.");

	      TextOutput out = new TextOutput(pathName + fileName);
	      out.println("pathName = '" + pathName + "'.");
	      out.println("fileName = '" + fileName + "'.");
	      out.close();

	      System.out.println("All done!");
	   }
	}
  1. Make sure that the drive letter (U:) in the file matches the drive that your project is on. (I.e. if you're working at home, you will want to change the U: to C: or whatever drive letter you are saving your work on.)
    A backslash — like is used in a file path — can't be used directly in a Java String. Java uses the backslash to mean “the next character means something special”. Doubling the backslash in Java indicates that “the next character (the second backslash) should be inserted into the String”.
  2. Run the program, and verify that it works. A file named fName_1.txt should be created in the same folder as the project file.
  3. Change the fileName to “fName_2.txt” and the pathName to “U:\\cs132\\W03\\L03\\”.
  4. When you run the program (don't do it yet!), where will the file be placed? Make a prediction. Find the names of several files and folders that are already in that directory and write them here.

  5. Run the program to verify that your prediction was correct.
    A path that starts with a disk drive letter is called an absolute path. An absolute path states exactly where the file will go, no matter which directory contains the main method.

Current Directories

  1. Change the fileName to “defDirFile.txt” and pathName to the empty String ("").
  2. Run the program. What is the path where the file was created? (Hint: look in directories related to this program.)
  3. What is the full path of the project file, FilePaths.jpr?
    The obvious relationship between these two is no accident! The program's current directory is the same directory that contains the project file. If you don't specify a file path, this is where the file will be created.

Relative Paths

A file can also be specified relative to the current directory, the directory containing the project file. Relative paths are like directions you might give someone: “to get there from here, you go north 3 blocks, turn right, go one block, and then go one more block north”. For a relative path, you might say “go up two directories, then down into the directory named ‘data’.”

The way to say “go up one directory” is with a “..” in the path (two dots means just one directory). The way to say “go down a directory” is to give the directory's name.

Do the following:

  1. In Windows Explorer, go to the directory named “W03”. In it, create a folder named “data”.
  2. In the Java program shown above, change pathName to “..\\..\\data\\” and the fileName to “fName_3.txt”.
  3. Run the program.
  4. What is the absolute path of the file created?

TextInput and TextOutput

File names and paths behave the same way whether you are writing a file with TextOutput or reading it with TextInput.