简体   繁体   中英

Eclipse Plugin: Copy File

i want to copy a file from a folder1 to folder2 in my eclipse project.

Here is my code:

IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject project = root.getProject(getSelectedProject().toString());

    IFolder folder = project.getFolder("www/GeneratedFiles");

    IFolder folder2 = project.getFolder("AppGenerator/TableFiles");

    IFile file = folder2.getFile("RelationsBC.bbTable");

    System.out.println("FileName: " + file.getName().toString());

    if (!project.exists())
        project.create(null);
    if (!project.isOpen())
        project.open(null);
    if (!folder.exists()) {
        folder.create(true, true, null);
        file.copy(folder.getFullPath(), true, null);
    } else {
        file.copy(folder.getFullPath(), true, null);
    }

When I run my Plugin, folder.create(true, true, null) works fine but file.copy(folder.getFullPath(), true, null); gives me an error.

org.eclipse.core.internal.resources.ResourceException: Resource '/todo/www/GeneratedFiles' already exists.

What I am doing wrong? Hope u can understand me fine.

The destination path parameter of copy should be the name of a file, you are using the name of the folder.

Use something like:

IPath path = folder.getFullPath();

path = path.append(file.getName());

file.copy(path, true, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM