我尝试为
Eclipse开发一个小插件,在几个文件夹(包)中创建几个Java文件,作为大型软件新模块的起点.
我试过像这样使用IFile对象:
final IFile file = container.getFile(new Path(myFileName)); ... file.create(stream,true,monitor);
只要文件路径上的所有文件夹都存在,该方法就可以正常工作.但它不会创建任何丢失的文件夹(新包),但会抛出“资源不存在”异常.
我无法通过IResource或IWorkspace对象找到任何方法.
解决方法
就个人而言,我使用一个小方法递归创建所有文件夹,如:
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); } }
这对我很有用.