1

我正在尝试在我的 Eclipse 插件项目中使用Properties 的方法。load()因为我想将属性文件放在这样的文件夹中:

Pluging Project/
               |
               +----/src/...   
               +----/config/config.properties
               +----/icons/...
               +----META-IN/
               +----build.properties
               +----plugin.xml

然后我尝试这样的代码但失败了

Properties prop = new Properties();
InputStream inputStream = (InputStream) Activator.getDefault().getBundle().getEntry("/config/config.properties").getContent();
prop.load(inputStream);

此方法接收输入字节流作为参数。而且我很确定会Activator.getDefault().getBundle().getEntry()返回一个 InputStream。如果我将属性文件放在调用类的同一位置并使用

InputStream inputStream = this.getClass().getResourceAsStream("config.properties");

它会顺利的。

那么有什么提示吗?

4

1 回答 1

1

URL返回的 by使用Bundle.getEntry内部 Eclipse 方案,并不总是支持getContents(). 你需要调用org.eclipse.core.runtime.FileLocator.toFileURL()来转换它:

URL url = Activator.getDefault().getBundle().getEntry("/config/config.properties");

url = FileLocator.toFileURL(url);

InputStream inputStream = (InputStream)url.getContent();

还要确保您有config目录中列出的build.properties

于 2013-10-25T09:07:33.853 回答