Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException in eclipse

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!

like image 808
DTRobertson94 Avatar asked Apr 10 '14 03:04

DTRobertson94


People also ask

What is filenotfoundexception in Eclipse?

1 java FileNotFoundException wont locate a file in the same project 1 Cannot access text file in java project 0 Eclipse oxygen, Java: cannot run project -1 Java Eclipse - The system cannot find the file specified ( java.io.FileNotFoundException )

Why do I get an exception when opening a file?

This exception mainly occurs for the below reasons: 1. If the application tries to open a file, but the file is not present in the desired location. 2. While creating the file, if there is a directory with the same name as the filename then this exception occurs.

How do you throw a filenotfoundexception?

The following constructors throw a FileNotFoundException when the specified filename does not exist: FileInputStream, FileOutputStream, and RandomAccessFile. These classes aim to obtain input bytes from a file in a file system, while the former class supports both reading and writing to a random access file.

How to handle the exception when there is no such file?

There are other remedies to handle the exception: If the message of the exception tells that there is no such file or directory, then you re-verify whether you mentioned the wrong file name in the program or file exists in that directory or not.


2 Answers

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.

like image 82
Franklin Yu Avatar answered Oct 02 '22 23:10

Franklin Yu


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.

like image 33
Justin Jasmann Avatar answered Oct 02 '22 23:10

Justin Jasmann