2012-05-10 44 views
0

爲什麼這段java代碼在我的主目錄中查找文件?爲什麼它不在我的eclipse工作區中(/ home/用戶/eclipse/workspace /)?Java URI類文件位置

import org.eclipse.emf.common.util.URI; 

URI uri = null; 
try { 
    uri = URI.createURI("../models/task.cm"); 
    Resource resource = resourceSet.getResource(uri, true); 
... 

回答

0

的getResource

資源的getResource( URI URI,布爾loadOnDemand)

Returns the resource resolved by the URI. 

資源集有望實現以下策略,以便將給定的URI解析爲資源。首先,它使用URI轉換器對URI進行規範化,然後將其與每個資源的規範化URI進行比較;如果發現匹配,則該資源成爲結果。如果沒有,它委託允許URI在其他地方解決。例如,程序包註冊表用於將程序包的名稱空間URI解析爲該程序包的靜態實例。所以重要的一點是任意實現可能會將URI解析爲任何資源,而不一定是包含在此特定資源集中的資源。如果委派步驟未能提供結果,並且如果loadOnDemand爲true,則創建資源並且該資源成爲結果。如果loadOnDemand爲true並且結果資源未加載,則它將在返回之前加載。

Parameters: 
    uri - the URI to resolve. 
    loadOnDemand - whether to create and load the resource, if it doesn't 
    already exists. 
Returns: 
    the resource resolved by the URI, or null if there isn't one and it's not 
    being demand loaded. 
Throws: 
    java.lang.RuntimeException - if a resource can't be demand created. 
    WrappedException - if a problem occurs during demand load. 

所以,找不到資源,並在您的主目錄中創建任意資源。

1

它不應該在工作空間目錄中查找文件。它應該查找當前工作目錄的父文件(因爲路徑以..開頭)。當您從eclipse運行時,當前工作目錄是您的項目的目錄。

我猜你的項目在你的主目錄下,所以這就是原因。

+0

但我的項目是在「/home/medy75/Eclipse/dev_workspace/cz.cvut.visualization」和前一個路徑(「../models/task.cm」)從「/ home/medy /」 。所以我不明白... – medy75

+0

@ medy75:這是一個RCP /插件應用程序?如果是這樣,你的臨時工作空間在哪裏創建(rcp_workspace左右)? – home

+0

是的,它應該是Eclipse插件和關於臨時工作空間我絕對不知道... – medy75

0

其實它是。在基於Unix的shell中,「..」代表當前工作的目錄。所以,它實際上是在圍繞Eclipse程序的文件夾中查看,或者正在運行的任何文件。

首頁將通過「//家」

當前工作目錄是由該值表示表示「」

用戶的目錄是由 「〜」

最後,根是由 「/」

例如表示表示:

Macintosh-2:/ Administration$ cd ~ #!sends me from root to my user directory 
Macintosh-2:~ Administration$ cd ../Administration #!sends me from my user directory, to the directory above it, then back down 
Macintosh-2:~ Administration$ cd Administration #!user directory doesn't exist inside my user directory, thus showing ".." represents the directory above the current working one. 
-bash: cd: Administration: No such file or directory 
Macintosh-2:~ Administration$ cd .. #!moves up a directory 
Macintosh-2:Users Administration$ #!now working in surrounding directory. 
+0

是的,我知道,但這段代碼並不尊重它。 「home/medy75/models」不在「/home/medy75/Eclipse/dev_workspace/cz.cvut ...」之上。 – medy75