开发者

Retrieve a file from within a folder in the workspace

I have a folder within my workspace but outside of 'src' which contains a file that needs to be read in order to setup a new file when开发者_如何学JAVA using a custom plugin wizard.

I cannot get the location of this file correct and keep getting null pointers unless I specify exactly where the file is in the system. My problem is the file is within a plugin project and I cannot get it's location.

The file location in the plugin is com.my.plugin/rules/setup.txt


To load a resource from your deployed bundle, you can do the following (the resource to load needs to be included in your build.properties binary build setup):

Bundle bundle = YourBundleActivator.getDefault().getBundle();
IPath path = new Path("rules/setup.txt");
URL setupUrl = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
File setupFile = new File(FileLocator.toFileURL(setupUrl).toURI());

Note that this is different from getting something from the workspace, as when your bundle is running, finding something in the workspace will refer to the runtime workspace, not the development workspace. If you do want something from the runtime workspace, you can access it like this:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resourceInRuntimeWorkspace = root.findMember("rules/setup.txt");
File file = new File(resourceInRuntimeWorkspace.getLocationURI());


Assuming you have something like:

  • workspace/src/Blah.java
  • workspace/plugin/commyplugin/rules/setup.txt

From Blah.java you should be able to do something like:

URL urlToFile = getClass().getResource("/plugin/commyplugin/rules/setup.txt");

And from there create a file or use the getResourceAsStream to return a InputStream.

I'm mostly guessing here :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜