代码之家  ›  专栏  ›  技术社区  ›  levik

获取Eclipse中当前编辑文件的绝对路径

  •  14
  • levik  · 技术社区  · 17 年前

    我想写一个插件来处理Eclipse中当前编辑的文件。但我不知道如何正确获取文件的完整路径。

    这就是我现在要做的:

    IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
        getAdapter(IFile.class);
    

    现在我有了一个ifile对象,我可以检索它的路径:

    file.getFullPath().toOSString();
    

    但是,这仍然只给了我相对于工作区的路径。我怎样才能从中得到绝对路径呢?

    6 回复  |  直到 12 年前
        1
  •  20
  •   Chris Marasti-Georg Scott Weinstein    12 年前

    看起来像你想要的 IResource.getRawLocation() . 返回一个 IPath ,也有一个 makeAbsolute() 方法,如果您想加倍确认您有一个绝对路径。

        2
  •  5
  •   Bitmap    14 年前

    我认为一个更适合Java的解决方案是使用以下内容:

    IResource.getLocation().toFile()
    

    这将利用iPath API(getLocation()部分)并返回java.io.file实例。当然,其他的答案也会让你找到你想去的地方。

    在一个切向的注释上,我发现了IDE类 (org.eclipse.ui.ide.IDE) 对于编辑器来说是一个有用的实用工具资源。

        3
  •  4
  •   ChrisJF    14 年前

    对我有用的答案(我测试过!)是:

    // Get the currently selected file from the editor
    IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
    if (file == null) throw new FileNotFoundException();
    String path = file.getRawLocation().toOSString();
    System.out.println("path: " + path);
    
        4
  •  1
  •   Todd Wallentine    17 年前

    我通常调用ifile.getLocation(),它返回ipath,然后调用ipath.toosString()。

    file.getLocation().toOSString()
    
        5
  •  0
  •   Bitmap    14 年前
    IWorkspace ws      = ResourcesPlugin.getWorkspace();  
    IProject   project = ws.getRoot().getProject("*project_name*");
    
    IPath location = new Path(editor.getTitleToolTip());  
    IFile file     = project.getFile(location.lastSegment());
    
    into file.getLocationURI() it's the absolute path
    
        6
  •  -2
  •   user3491194    12 年前

    对我来说,这次跑步不错。

    iWorkSpaceRoot WorkspaceRoot=ResourcesPlugin.GetWorkspace().GetRoot();

    文件文件=workspaceroot.getrawlocation().makeAbsolute().tofile();

    此位置的文件列表:

    file[]files=file.listfiles();