How to Troubleshoot Java Problems with Disk Files

By Barry Burd

If the Java virtual machine can’t find the file you’ve asked it to find, you see an error message. This error message can be very frustrating. In many cases, you know darn well that there’s a rawData.txt file on your hard drive. The stupid computer simply can’t find it.

image0.jpg

There’s no quick, surefire way to fix this problem. But you should always check the following things first:

  • Check again for a file namedrawData.txt.

    Open My Computer (on Windows) or Finder (on a Mac) and poke around for a file with that name.

    The filenames displayed in My Computer and Finder can be misleading. You may see the name rawData, even though the file’s real name is rawData.txt.

  • Check the spelling of the file’s name.

    Make sure that the name in your program is exactly the same as the name of the file on your hard drive. Just one misplaced letter can keep the computer from finding a file.

  • If you use Linux (or a flavor of UNIX other than Mac OS X), check the capitalization of the file’s name.

    In Linux, and in many versions of UNIX, the difference between uppercase and lowercase can baffle the computer.

  • Check that the file is in the correct directory.

    Sure, you have a file named rawData.txt. But don’t expect your Java program to look in every folder on your hard drive to find the file. How do you know which folder should house files like rawData.txt?

    Here’s how it works: Each Eclipse project has its own folder on your computer’s hard drive. You see the 13-02 project folder and its src subfolder above. But below, Windows Explorer shows the 13-02 folder, its src subfolder, and its other subfolders named .settings and bin. (Mac users can see the same subfolders in a Finder window.)

    The src, bin and .settings folders contain files of their own. The rawData.txt and cookedData.txt files are immediately inside the 13-02 project folder. In other words, the rawData.txt and cookedData.txt files live in the root of the 13-02 project folder.

    When you run this example, the rawData.txt file should be in the root of the 13-02 project folder on your hard drive.

    In general, file locations can be tricky, especially if you switch from Eclipse to an unfamiliar IDE. The general rule (about putting input and output files immediately inside a project directory) may not apply in other programming environments.

    So here’s a trick you can use: Whatever IDE you use (or even if you create Java programs without an IDE), run this stripped-down version of code:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    class JustWrite {
     public static void main(String args[])
      throws FileNotFoundException {
      PrintStream diskWriter =
       new PrintStream("cookedData.txt");
      diskWriter.println(99.75);
      diskWriter.close();
     }
    }

    This program has no need for a stinking rawData.txt file. If you run this code and get no error messages, search your hard drive for this program’s output (the cookedData.txt file). Note the name of the folder that contains the cookedData.txt file. When you put rawData.txt in this same folder, any problem you had running the Listing 13-2 code should go away.

    image1.jpg

  • Check the rawData.txt file’s content.

    It never hurts to peek inside the rawData.txt file and make sure that the file contains the numbers 19.95 5. If rawData.txt doesn’t appear in Eclipse’s editor area, find the project in the Package Explorer. Double-clicking the project’s rawData.txt branch makes that file appear in Eclipse’s editor area.

    By default, Java’s Scanner class looks for blank spaces between input values. So this example’s rawData.txt file should contain 19.95 5, not 19.955 and not 19.95,5.

    The Scanner class looks for any kind of whitespace between the values. These whitespace characters may include blank spaces, tabs, and newlines. So, for example, the rawData.txt file may contain 19.95 5 (with several blank spaces between 19.95 and 5), or it may have 19.95 and 5 on two separate lines.