如何通过插件在Eclipse项目中创建包(文件夹) [英] How to create packages (folders) in an Eclipse project via plugin
问题描述
我试图开发一个Eclipse的小插件,以便在多个文件夹(包)中创建几个Java文件,作为大型软件的新模块的起点。
I try to develop a small plugin for Eclipse to create several Java files in several folders (packages) as a starting point for a new module of a larger software.
我试图使用这样的 IFile
对象:
I've tried to use an IFile
object like this:
final IFile file = container.getFile(new Path(myFileName));
...
file.create(stream, true, monitor);
只要文件路径上的所有文件夹都存在,它就可以工作。但是它不会创建任何丢失的文件夹(新的包),而是抛出一个资源不存在异常。
That works as long as all folders on the path to the file exists. But it does not create any missing folders (new packages) but throws a "resource not exists" exception.
我找不到任何方法$ c> IResource 或 IWorkspace
对象
I could not find any way to do this by IResource
or IWorkspace
objects.
推荐答案
个人而言,我使用一种递归创建所有文件夹的小方法,如:
Personally, I use a small method which recursively creates all of the folders, something like:
IFile file = project.getFile(newPath);
prepare((IFolder) file.getParent());
然后方法
public void prepare(IFolder folder) {
if (!folder.exists()) {
prepare((IFolder) folder.getParent())
folder.create(false, false, null);
}
}
这对我很好。
这篇关于如何通过插件在Eclipse项目中创建包(文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!