[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]Eclipse RCP plugin开发,取得插件目录下的配置文件

上一篇:[整理]Java线程:Thread.currentThread()与this的关系
下一篇:[整理]ANT批处理,更新zip包的几个用法

添加日期:2011/6/29 10:40:01 快速返回   返回列表 阅读4944次
PLUGIN_NAME就是插件ID
插件要解开,以文件夹的形式放到Plugins下面,不能以Jar的形式放,那样无法更新properties文件。


/**
 * 插件配置文件取得.
 * 
 * @param resourceFilePath
 *            配置文件在插件中的相对路径
 * @return 文件对象
 * @throws IOException
 *             配置文件取得失败
 */
private static File getConfigFile(String resourceFilePath)
        throws IOException {

    // 取得资源URL,形式如bundleresource://8.fwk4898828:6/abc.properties
    URL url = Platform.getBundle(PLUGIN_NAME).getResource(resourceFilePath);
    if (url == null) {
        throw new IOException(resourceFilePath + "文件未找到。");
    }

    // 转为文件对象
    File file = null;
    try {

        // 转换为文件路径,如file:/E:/aaa 桌面/plugins/xxx/abc.properties
        url = FileLocator.resolve(url);

        // 路径中可能存在特殊字符(如空格),需要转换为%xx形式,使用URI的构造方法可以自动转换。
        // windows文件夹名不允许的字符:\/:*?"<>|
        // 可能使用的特殊字符:`-=~!@$%^&()_+[]{};',.#
        // 路径名包含这四个字符“@#%;”时,整个程序不能启动,其它字符都OK
        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
                url.getQuery(), null);
        log.debug(resourceFilePath + ":" + uri);

        // 通过文件方式存取。插件要导出为目录的形式,不要导出Jar包的形式,后者无法更新配置文件.
        file = new File(uri);
    } catch (URISyntaxException e) {
        throw new IOException(resourceFilePath + "文件取得失败。", e);
    }

    // 返回
    return file;
}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved