Question

Code:

import java.io.*;
import java.util.Scanner;

public class Driver {

    private int colorStrength;
    private String color;

    public static void main(String[] args) throws IOException {

        String line, file = "strength.txt";

        File openFile = new File(file);
        Scanner inFile = new Scanner(openFile);

        while (inFile.hasNext()) {
            line = inFile.nextLine();
            System.out.println(line);
        }

        inFile.close();
    }
}

This is a small part of a program I am writing for a class (the two private attributes have yet to be used I know) but when I try to run this with the strength.txt file I receive the following errors:

Exception:

Exception in thread "main" java.io.FileNotFoundException: strength.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Driver.main(Driver.java:14)

If anyone with Eclipse could help me figure this out it would be much appreciated!

Was it helpful?

Solution

You've used a relative file path which is relative to your project execution.

If you'd like to do it that way, simply put the strength.txt file in the base directory of your project. Like so:

enter image description here

Alternatively, you could reference the absolute file path on your system. For example, use:

Windows:

C:/dev/myproject/strength.txt

Mac/Unix:

/Users/username/dev/strength.txt

(or whatever the full path may be) instead.

OTHER TIPS

Do this

System.out.println(openFile.getAbsolutePath());

It will show you where JVM expects to find the file and whether it is the folder you expect as well, Accordingly place the file or give the exact location

Use this to see what file path the system is using to reach the relative path

    System.out.print(System.getProperty("user.dir"));

Then make sure that the relative path immediately follows this path.

You can also turn that into a string by doing

String filePath = System.getProperty("user.dir");

and then you can just add that to the beginning of the filepath like so,

    ImageIcon imageIconRefVar = new ImageIcon(filePath + "/imagepathname");

I found this solved the issue for me when I used it in the path (which seemed odd since that should be the location it is in, but it worked)

You've used a relative file path which is relative to your project execution.

If this works for you, you can change the execution directory from the project root to the binary directory by:

  1. "Run" -> "Run configurations"
  2. In the "Arguments" tab, find "working directory" settings.
  3. Switch from "Default" to "Other".
  4. Click the "Workspace" button and select the project in pop-up window then click "OK"; this will bring you something like $(workspace_loc:proj-1).
  5. Append "/bin" to the end of it; save the configuration.

I need this instead of simply putting files in project root directory when I am doing assignment and the professor requires specific file hierarchy; in addition, this is more portable than absolute path.

Inside the base directory create a folder, name it "res". Place your file inside "res" folder.

use String file = ".\\res\\strength.txt"; to reference the location of your file.

You should use a resource folder to store all the files you use in your program (a good practice).

And make a refrence of that file from your root directory. So the file is present within the package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top