Java 为什么Eclipse插件开发中的属性加载方法失败?
我正试图在我的Eclipse插件项目中使用这个方法。因为我想将属性文件放在如下文件夹中:Java 为什么Eclipse插件开发中的属性加载方法失败?,java,properties,eclipse-plugin,Java,Properties,Eclipse Plugin,我正试图在我的Eclipse插件项目中使用这个方法。因为我想将属性文件放在如下文件夹中: Pluging Project/ | +----/src/... +----/config/config.properties +----/icons/... +----META-IN/ +----build.propert
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");
会很顺利的
有什么提示吗?由
捆绑包返回的URL
。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();
另外,请确保在
build.properties
中列出了config
目录,该URL
由捆绑包返回。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();
另外,请确保在build.properties
中列出了config
目录。是否尝试使用URL
的getContent()
?是否尝试使用URL
的getContent()
?