Java workspace and file path

烈酒焚心 提交于 2019-12-09 13:23:12

问题


I have a probably easy to solve problem. I am having a folder in my project and want to get it using the relative path:

new File("/folder")

this gives me FileNotFoundException

if I try like this

new File("d:/workspace/project/folder")

it works

I suspect that it does not work because of this: new File("").getAbsolutePath() returns : D:\eclipse So not the path of the workspace.

Am I doing something wrong or do I need to change some settings in eclipse


回答1:


Just found my answer in Run Cofigurations like djna suggested, but not in the Environment Tab but in the Arguments Tab. There is a working directory section in which d:\eclipse was set and which needed to be set to ${workspace_loc:myproject}




回答2:


Don has pointed that you use a absolute path. That is the problem.

By default the working directory in Eclipse is the path of the project where is the program that you execute.

You can always know the working directory by displaying this property :

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



回答3:


new File("/folder")

is not a relative path, it is an absolute one. If you want to access the relative Path use

new File("folder")

or

new File("./folder")



回答4:


In the run configurations you can specify the working directory for the launch. This might vary in different Eclipses but for me:

Run->Run Cofigurations ...
Select Java Application, right Click New
Environment Tab gives you a chance to specify the working directory
    Select Other, and then Workspace ... to specify a project in your workspace



回答5:


Can u try this?

File file = new File("folder");
String path = file.getAbsolutePath();
FileInputStream fis = new FileInputStream(path);



回答6:


D:\eclipse is the root folder of your project. If you create another project in D:\java in the same workspace, new File("").getAbsolutePath() will return D:\java so this is independent of the path of your workspace.

If you want a folder inside your project, use ., example:

System.out.println(new File(".\\test").getAbsolutePath());


来源:https://stackoverflow.com/questions/16152857/java-workspace-and-file-path

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