String path = Platform.getBundle("你的项目名称").getLocation();
path = path.substring(path.indexOf("/"), path.length()) + "根目录下的文件名称";
document = reader.read(new File(path));
上面这种方法变异运行没有问题,但是打包生成插件之后运行,会报错(空指针)
下面这种完美
例子1:
InputStream is = CommonUtil.class.getResourceAsStream("/youxml.xml");
document = reader.read(is);
例子2:
InputStream is = CommonUtil.class.getResourceAsStream("/text.txt");
reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
while((line = reader.readLine()) != null){
}
小结:
String path = getResource("/th.txt").getPath();
File file = new File(path);// 不行 这样得到的不是项目下的文件
InputStream is = getResourceAsStream("/th.txt"); // 当前项目根目录下的th.txt