lev*_*vik 14 java eclipse eclipse-api eclipse-plugin
我想编写一个插件,它与Eclipse中当前编辑的文件做了一些事情.但我不确定如何正确获取文件的完整路径.
这就是我现在所做的:
IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
getAdapter(IFile.class);
Run Code Online (Sandbox Code Playgroud)
现在我有一个IFile对象,我可以检索它的路径:
file.getFullPath().toOSString();
Run Code Online (Sandbox Code Playgroud)
然而,这仍然只给我相对于工作空间的路径.我怎样才能从中得到绝对的路径?
我认为更友好的Java解决方案是使用以下方法:
IResource.getLocation().toFile()
Run Code Online (Sandbox Code Playgroud)
这利用了IPath API(getLocation()部分)并将返回一个java.io.File实例.当然,其他答案可能会让你到达你想要的地方.
在切线上,我发现IDE类(org.eclipse.ui.ide.IDE)在编辑器方面是一个有用的实用程序资源.
对我有用的答案(我测试过!)是:
// 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20101 次 |
| 最近记录: |