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

Java 获取Eclipse中当前编辑的文件的绝对路径,java,eclipse,eclipse-plugin,eclipse-api,Java,Eclipse,Eclipse Plugin,Eclipse Api,我想写一个插件来处理Eclipse中当前编辑的文件。但我不确定如何正确获取文件的完整路径 这就是我现在所做的: IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput(). getAdapter(IFile.class); 现在我有了一个IFile对象,我可以检索它的路径: file.getFullPath().toOSString(); 但是,这仍然只提供了相对于工作区的路径。如何从中获取

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

这就是我现在所做的:

IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
    getAdapter(IFile.class);
现在我有了一个IFile对象,我可以检索它的路径:

file.getFullPath().toOSString();

但是,这仍然只提供了相对于工作区的路径。如何从中获取绝对路径?

看起来像是您想要的。这将返回一个
IPath
,如果您想双重确保您拥有一个绝对路径,它还具有一个
makeAbsolute()
方法。

我通常调用IFile.getLocation(),它返回一个IPath,然后调用IPath.toOSString()


我认为更为Java友好的解决方案是使用以下方法:

IResource.getLocation().toFile()
这将利用IPath API(getLocation()部分),并将返回一个java.io.File实例。当然,其他答案可能也会让你达到你想要的程度


顺便说一句,我发现IDE类
(org.eclipse.ui.IDE.IDE)
对于编辑器来说是一个有用的实用工具资源。

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

对我来说,这次跑步还可以

IWorkspaceRoot workSpaceRoot=ResourcesPlugin.getWorkspace().getRoot()

File File=workSpaceRoot.getRawLocation().makeAbsolute().toFile()

此位置的文件列表:


File[]files=File.listFiles()

来自IResource.getRawLocation()的链接现在已更改。
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
IResource.getLocation().toFile()
// 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);