Java – how to create packages (folders) in eclipse projects through plug-ins

I try to develop a small plug-in for eclipse and create several java files in several folders (packages) as the starting point for new modules of large software

I've tried using ifile objects like this:

final IFile file = container.getFile(new Path(myFileName));
...
file.create(stream,true,monitor);

This method works as long as all folders on the file path exist However, it will not create any missing folders (new packages), but will throw a "resource does not exist" exception

I can't find any methods through iresource or iworkspace objects

Solution

Personally, I use a small method to recursively create all folders, such as:

IFile file = project.getFile(newPath);

prepare((IFolder) file.getParent());

Then there is the method

public void prepare(IFolder folder) {
    if (!folder.exists()) {
        prepare((IFolder) folder.getParent())
        folder.create(false,false,null);
    }
}

This is very useful to me

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>