2013-10-25 22 views
1

我想在我的Eclipse插件項目中使用Properties's load()方法。因爲我想把對propertie文件的文件夾中是這樣的:爲什麼Eclipse插件開發中的屬性加載方法失敗?

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。 如果我把propertie文件中調用類的同一個位置,並使用

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

,便能很好。

因此,任何提示?

+2

列出的config目錄您是否嘗試過使用[的OpenStream()](http://docs.oracle.com /javase/6/docs/api/java/net/URL.html)而不是'URL'的'getContent()'? – Katona

回答

1

返回的URL使用內部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

+0

您的方式和@Katona評論workstions.Tks –