从Eclipse plugin中读文件

进行Eclipse插件开发或者RCP开发的时候,常常遇到这样的需求:程序员准备了几个文件打在jar包里,在程序运行的时候由用户的操作触发,要读jar包里的这几个文件,显示内容在界面上,或者直接复制文件到用户的目录中。这里提供两种直截了当的方法来实现这一目的。

第一,使用OSGi自带的utility class / methods,例子中的com.company.example是bundle (或者plugin) 的id,要读的文件是这个bundle中 resources文件夹中的 backup.txt 文件。

  1. Bundle bundle = Platform.getBundle("com.company.example");  
  2. URL fileURL = bundle.getEntry("resources/backup.txt");  
  3. File file = null;  
  4. try {  
  5.     file = new File(FileLocator.resolve(fileURL).toURI());  
  6. catch (URISyntaxException e) {  
  7.     e.printStackTrace();  
  8. catch (IOException e) {  
  9.     e.printStackTrace();  
  10. }  

第二,不依赖OSGi Platform,直接使用platform协议(platform:/plugin)生成URL,例子中的com.company.example是bundle (或者plugin) 的id,要读的文件是这个bundle中 resources文件夹中的 backup.txt 文件。
  1. URL url;  
  2. try {  
  3.     url = new URL("platform:/plugin/com.company.example/resources/backup.txt");  
  4.     InputStream inputStream = url.openConnection().getInputStream();  
  5.     BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));  
  6.     String inputLine;  
  7.     while ((inputLine = in.readLine()) != null) {  
  8.         System.out.println(inputLine);  
  9.     }  
  10.     in.close();  
  11. catch (IOException e) {  
  12.     e.printStackTrace();  
  13. }  


如果要拷贝文件到Eclipse RCP项目 (生成IFile) 的话,直接用org.eclipse.core.resources.IFile的

void create(InputStream source,
            int updateFlags,
            IProgressMonitor monitor)
            throws CoreException

方法以及利用URL.openConnection()得到的 InputStream来生成。


第二种方法比第一种有几个优点:

1、不需要import org.eclipse.core.runtime,org.osgi.framework包,

或者depend on org.eclipse.core.runtime这个bundle

2、org.osgi.framework.Bundle的 getEntry(String path) 方法对于不同的文件系统协议有不同的处理方式,比如file:/, jar:/, zip:/ 都有细微差别

从下面粘贴的官方文档中模棱两可的解释就可见一斑,一不小心就可能出错,而且在不同环境中可能会出现意想不到的bug (比如在Eclipse环境下工作的好好的,各个bundle都打成jar包以后可能就会路径报错)


getEntry

java.net.URL getEntry(java.lang.String path)
Returns a URL to the entry at the specified path in this bundle. This bundle's class loader is not used to search for the entry. Only the contents of this bundle are searched for the entry.

The specified path is always relative to the root of this bundle and may begin with "/". A path value of "/" indicates the root of this bundle.

Note: Jar and zip files are not required to include directory entries. URLs to directory entries will not be returned if the bundle contents do not contain directory entries.

Parameters:
path - The path name of the entry.
Returns:
A URL to the entry, or  null if no entry could be found or if the caller does not have the appropriate AdminPermission[this,RESOURCE] and the Java Runtime Environment supports permissions.
Throws:
java.lang.IllegalStateException - If this bundle has been uninstalled.
Since:
1.3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值