从 Eclipse 插件中的文件读取

use*_*404 5 java eclipse eclipse-plugin

我需要从 eclipse 的属性文件中读取配置详细信息。我已将其放在与 我调用的类文件config.properties相同的级别plugin.xml中:

Properties properties = new Properties();
FileInputStream file;
String path = "./config.properties";
file = new FileInputStream(path);
properties.load(file);
Run Code Online (Sandbox Code Playgroud)

我得到一个file not found exception. 有没有更好的方法来做到这一点?

Nie*_*sen 5

你记得把它包含在构建中吗?

其次,无论如何使用类加载器资源可能更好

InputStream fileStream = myClass.getResourceAsStream( "/config.properties" );
Run Code Online (Sandbox Code Playgroud)

此外,还有另一种在 Eclipse 中使用打开资源 URL 的方法

url = new URL("platform:/plugin/com.example.plugin/config.properties");
InputStream inputStream = url.openConnection().getInputStream();
Run Code Online (Sandbox Code Playgroud)