Java URI类文件位置

时间:2012-05-10 15:11:09

标签: java eclipse uri workspace home-directory

为什么这段java代码在我的主目录中查找文件?为什么它不在我的eclipse工作区(/ home / user / eclipse / workspace /)?

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

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

3 个答案:

答案 0 :(得分:1)

它不应该在工作区目录中查找文件。它应该查找当前工作目录中的父文件(因为路径以..开头)。从eclipse运行时的当前工作目录是项目的目录。

我猜你的项目在你的主目录下,所以这就是原因。

答案 1 :(得分:0)

实际上是。在基于Unix的shell中,“..”表示当前工作的目录。因此,它实际上是在查看Eclipse程序周围的文件夹,或者正在运行的任何内容。

Home将由“// home”代表

当前工作目录由“。”

的值表示

用户的目录由“〜”

表示

最后,根用“/”

表示

例如:

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.

答案 2 :(得分:0)

的getResource

资源getResource(URI uri,boolean 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.

因此,找不到资源,并在主目录中创建任意资源。