从工作空间中的文件夹中检索文件 [英] Retrieve a file from within a folder in the workspace
问题描述
我的工作区内有一个文件夹,但在src之外,包含需要读取的文件,以便在使用自定义插件向导时设置新文件。
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 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.
插件中的文件位置是com.my.plugin / rules / setup.txt
The file location in the plugin is com.my.plugin/rules/setup.txt
推荐答案
要从已部署的包中加载资源,您可以执行以下操作(要加载的资源需要包含在您的 build.properties
binary build setup):
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());
这篇关于从工作空间中的文件夹中检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!