简体   繁体   中英

Reading from file in Eclipse plugin

I need to read the configuration details from a properties file in eclipse. I have put the config.properties at the same level as plugin.xml and in the class file I call:

Properties properties = new Properties();
FileInputStream file;
String path = "./config.properties";
file = new FileInputStream(path);
properties.load(file);

I get a file not found exception . Is there a better way of doing this?

Did you remember to include it in the build?

Secondly, using the classloader resource is probably better anyway

InputStream fileStream = myClass.getResourceAsStream( "/config.properties" );

Also, there is another way of opening a resource URL in eclipse using

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

Put the properties file in the root of your project. This should be where the user.dir system property is pointing. The FileInputStream constructor looks in this directory for the file.

You can confirm it is in the correct directory by outputting the System Property.

System.getProperty("user.dir");

If it is a plugin, you should tell it that your file must be included into the target. It is set in the build.properties file. Open it and look - you haven't your file mentioned there.

Open plugin.xml, go to build tab and include your file in the binary.build by checking it. Save. That will change the build.properties file correctly. Go to build.properties tab and look at the file again - now you see your file included correctly.

Of course, you could do it by hand, but plugin UI is convenient and you can be sure you haven't any problem ends of line or spaces.

For absolute addressing the files in the plugins look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM