如何使用java从e​​clipse获取当前项目路径

时间:2016-08-31 12:04:51

标签: java eclipse

我在eclipse中有一个名为SampleProj的项目。

项目结构如下:

SampleProj
   |  
    -- META-INF
            |
             -- dbdetails.properties

目前,我正在阅读这样的文件:

FileReader reader = new FileReader("C://Workspace//SampleProj/META-INF//dbdetails.properties");

但是这条路径是我系统rit的本地路径。所以,我想让这条路径变得通用。

所以,我试图保持这样的道路:

FileReader reader = new FileReader("SampleProj/META-INF//dbdetails.properties");

但是,我找不到文件异常。

如何从Project中读取此文件。任何人都可以帮我解决这个问题......

4 个答案:

答案 0 :(得分:2)

You can retrieve using :

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

Then you can do it like this :

FileReader reader = new FileReader(workingDir +"//SampleProj//META-INF//dbdetails.properties");

答案 1 :(得分:1)

  • Go to Run/Run Configurations...
  • Select the Launch Configuration that you want to use
  • Select the tab Arguments
  • In the Working directory section, click Other
  • Put C:\Workspace in the text field
  • Click Apply

Then SampleProj/META-INF/dbdetails.properties should work as path since a relative path is relative to the user working directory corresponding to the System property user.dir.

答案 2 :(得分:0)

As fas as your development is concerned you can do it like below but once you deploy the project its better to add file in classpath

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


FileReader reader = new FileReader(currentDirectory+"/SampleProj/META-INF//dbdetails.properties");

答案 3 :(得分:0)

IF your using spring web-mvc project use this samplecode:

String path=session.getServletContext().getRealPath("/ui_resources/images/logo.png");