從插件中獲取資源的幾種方式[總結]_FileLocator

方式一:
public static URL getFile(String filename) throws IOException {
  System.out.println("get file "  + filename);
  //URL installURL = EditorPlugin.getDefault().getDescriptor().getInstallURL();
  URL installURL = Platform.getBundle("cbg.editor").getEntry("/");//獲取id爲"cbg.editor"插件的位置
  System.out.println("install url " + installURL);
  //URL mode = Platform.resolve(new URL(installURL, filename));
  return FileLocator.resolve(new URL(installURL, filename));
}

eg:
String filename = "template.xml";
URL url = getFile.getFile("modes/" + filename);//相對路徑

方式二:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");//注意這裏是傳的相對路徑
URL fileURL = FileLocator.find(bundle, path, null);
InputStream in = fileURL.openStream();

方式三:
InputStream is  is = EditorPlugin.getDefault().getBundle().getEntry("icons/project.gif").openStream();//傳的也是相對路徑

方式四:
URL url = CorePlugin.getDefault().getBundle().getResource("log4j.properties");
獲取CorePlugin插件根目錄下的"log4j.properties"文件

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章