现在的位置: 首页 > 综合 > 正文

Eclipse 中如何取得资源路径信息

2017年05月21日 ⁄ 综合 ⁄ 共 787字 ⁄ 字号 评论关闭

  有下面几种方法:

  1.通过bundle来访问:

   

Bundle bundle = Platform.getBundle("de.vogella.example.readfile");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
    file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

 

  2.通过 Eclipse 的URL 机制:

URL url;
try {
        url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
        InputStream inputStream = url.openConnection().getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        String inputLine;
 
        while ((inputLine = in.readLine()) != null) {
           System.out.println(inputLine);
        }
 
        in.close();
 
} catch (IOException e) {
    e.printStackTrace();
}

    在第二种方案中,如果需要将URL 转化成File,就需要使用FileLocator.resolve方法来转换:

    

file = new File(FileLocator.resolve(fileURL).toURI());

抱歉!评论已关闭.