2012-05-10 48 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