Extract a file from a folder in the workspace

I have a folder in my workspace, but outside of "DSI", which contains a file that must be read in order to install a new file using the custom plug-in wizard.

I can’t get the location of this file correctly and continue to get null pointers unless I specify exactly where this file is on the system. My problem is that the file is in the plugin project and I cannot find it.

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

+4
source share
2 answers

To load a resource from a deployed package, you can do the following (the resource for loading must be included in your binary build.properties ):

 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()); 

Please note that this is different from getting something from the workspace, because when your package is running, searching for something in the workspace refers to the workspace of the workspace, not the development workspace. If you want something from the runtime workspace, you can access it as follows:

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

Assuming you have something like:

  • Workspace /SRC/Blah.java
  • Workspace / plugin / commyplugin / rules / Setup.txt

From Blah.java you can do something like:

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

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

Mostly I guess :)

0
source

Source: https://habr.com/ru/post/1299570/


All Articles