java.io.FileNotFoundException in eclipse
Asked Answered
E

5

10

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!

Extend answered 10/4, 2014 at 3:33 Comment(5)
Is strength.txt co-located with Driver.class?Kuykendall
Yes it is. I have it saved in the src folder of the program as well as the Project folderExtend
It is not. @PM77-1 asked you to check if that is co-located with Driver.class. If you look at the bin directory, you would have seen only Driver.class unless you make the below mentioned change in either of the answers.Peony
It is in the bin directory along with the Driver.class and still doesn't workExtend
OK. Check the spelling.Kuykendall
M
14

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.

Mallet answered 10/4, 2014 at 3:43 Comment(3)
I put the file into the base directory as you said and it still gives me the same error code. Do you have any other tips?Extend
Hmm, works for me. Anyways, use the full path on your system then. Keep in mind that file paths are system dependent, so running it on a Windows machine will have different results than a Mac/Unix machine.Mallet
It should have worked. Try printing the absolute path as i mentioned below, and tell us what u getPeony
P
3

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

Peony answered 10/4, 2014 at 3:42 Comment(1)
If you do the above , you see that your file is in say C:\Harley\Test\strength.txt ie: it is expecting the file in the project base directory.Peony
I
2

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)

Instigation answered 10/4, 2014 at 4:11 Comment(0)
T
2

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.

Turnip answered 27/1, 2016 at 21:35 Comment(0)
L
0

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.

Lunkhead answered 10/4, 2014 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.