获取Eclipse中当前编辑文件的绝对路径
我想编写一个插件来对 Eclipse 中当前编辑的文件执行某些操作。 但我不确定如何正确获取文件的完整路径。
这就是我现在所做的:
IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
getAdapter(IFile.class);
现在我有一个 IFile 对象,我可以检索它的路径:
file.getFullPath().toOSString();
但是,这仍然只给我相对于工作区的路径。 我怎样才能从中获得绝对路径?
I'd like to write a plugin that does something with the currently edited file in Eclipse. But I'm not sure how to properly get the file's full path.
This is what I do now:
IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
getAdapter(IFile.class);
Now I have an IFile object, and I can retrieve it's path:
file.getFullPath().toOSString();
However this still only gives me the path relative to the workspace. How can I get the absolute path from that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来你想要
IResource.getRawLocation()
。 它会返回一个IPath
,如果您想双重确定自己拥有绝对路径,它还有一个makeAbsolute()
方法。Looks like you want
IResource.getRawLocation()
. That returns anIPath
, which also has amakeAbsolute()
method if you want to be doubly sure you've got an absolute path.我认为一个对 Java 更友好的解决方案是使用以下内容:
这利用了 IPath API(getLocation() 部分)并将返回一个 java.io.File 实例。 当然,其他答案也可能会让您到达您想要的地方。
顺便说一句,我发现 IDE 类
(org.eclipse.ui.ide.IDE)
对于编辑器来说是一个有用的实用资源。I think a more Java friendly solution would be to do use the following:
This takes advantage of the IPath API (the getLocation() part) and will return a java.io.File instance. Of course the other answers will probably get you to where you want to be too.
On a tangential note, I find the IDE class
(org.eclipse.ui.ide.IDE)
a useful utility resource when it comes to editors.对我有用的答案(并且我测试了它!)是:
The answer that worked for me (and I tested it!) was:
我通常调用 IFile.getLocation() 返回 IPath,然后调用 IPath.toOSString()。
I usually call IFile.getLocation() which returns an IPath and then call IPath.toOSString().
对我来说,这运行正常。
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
文件文件 = workSpaceRoot.getRawLocation().makeAbsolute().toFile();
此位置的文件列表:
File[] files = file.listFiles();
For me, this run ok.
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
File file = workSpaceRoot.getRawLocation().makeAbsolute().toFile();
file list from this location:
File[] files = file.listFiles();