java.io.FileNotFoundException in eclipse

≡放荡痞女 提交于 2019-11-28 01:02:51

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:

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!