2013-06-21 124 views
0

我想從我的JAVA程序中訪問config.xml文件。但它顯示我「文件未找到」。 這是我的項目結構。我正在使用eclipse。JAVA中的相對路徑設置

enter image description here

所以現在我要讀 「的conf /配置-file.xml」。我應該怎麼做?
備註:

  • 我無法更改項目結構。
  • 'conf'和'bin'它們不在'/root'下面。它們只在一個共同的項目目錄下。

請幫忙 謝謝!

+1

嘗試獲取jar文件的路徑,也許[本文] [1]會有幫助。 [1]:http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file –

回答

2

使用Java系統變量的「user.dir」屬性,您可以訪問您的文件。

System.getProperty("user.dir") + File.separator + "conf" + File.separator + "config-file.xml" 
+1

謝謝..這個工程。但我必須更改我的Eclipse我的默認設置位置。 – SRJ

0

不知道,但嘗試這個>> ../conf/config-file.xml

1

如果您從Eclipse的Java程序,默認的工作目錄(當前目錄)就像是「C項目目錄: \ workspace \ myproject「(或Linux上的/ workspace/myproject)。要打開conf/config-file.xml,下面的代碼應該可以工作。

File configFile = new File("conf/config-file.xml"); 
System.out.println(configFile.getAbsolutePath()); // Just for testing