如何以编程方式将文件夹从插件复制到工作空间中的新项目? [英] How to programmatically copy a folder from a plugin to a new project in the work space?

查看:105
本文介绍了如何以编程方式将文件夹从插件复制到工作空间中的新项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Eclipse插件,创建一个新的项目向导。在工作空间中创建这样的新项目时,我需要它将文件夹及其后代从插件复制到工作区中刚创建的项目。问题是,当项目是一个 IResource 时,插件文件夹在文件系统中。



我成功地获取了我需要复制的源代码插件文件夹的URL,并且我有IProject引用。



我需要知道的是:如何将前者复制到后者?

解决方案

查看此答案,了解如何获取文件/文件夹脱离插件。



然后在项目中创建新文件/文件夹,并使用 InputStream 设置文件内容:

  void copyFiles(File srcFolder,IContainer destFolder){
for(File f:srcFolder.listFiles()) {
if(f.isDirectory()){
IFolder newFolder = destFolder.getFolder(new Path(f.getName()));
newFolder.create(true,true,null);
copyFiles(f,newFolder);
} else {
IFile newFile = destFolder.getFile(new Path(f.getName()));
newFile.create(new FileInputStream(f),true,null);
}
}
}


I am developing an Eclipse Plugin creating a new Project Wizard. When creating such new project in the workspace I need it to copy a folder, and its descendent, from the plugin to the just created project in the workspace. The problem is that while the project is an IResource the plugin folder is in the file system.

I succeeded in getting an URL for the source plugin folder I need to copy and I have the IProject reference.

What I need to know is: How to copy the former into the latter?

解决方案

Check out this answer to see how to get a file/folder "out of" a plugin.

Then create new files/folders in the projects and set file contents using InputStream:

void copyFiles (File srcFolder, IContainer destFolder) {
    for (File f: srcFolder.listFiles()) {
        if (f.isDirectory()) {
            IFolder newFolder = destFolder.getFolder(new Path(f.getName()));
            newFolder.create(true, true, null);
            copyFiles(f, newFolder);
        } else {
            IFile newFile = destFolder.getFile(new Path(f.getName()));
            newFile.create(new FileInputStream(f), true, null);
        }
    }
}

这篇关于如何以编程方式将文件夹从插件复制到工作空间中的新项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆