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.
我想编写一个插件,用Eclipse中当前编辑的文件做一些事情。但我不确定如何正确获取文件的完整路径。
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:
现在我有一个IFile对象,我可以检索它的路径:
file.getFullPath().toOSString();
However this still only gives me the path relative to the workspace. How can I get the absolute path from that?
然而,这仍然只给我相对于工作空间的路径。我怎样才能从中得到绝对的路径?
6 个解决方案
#1
20
Looks like you want IResource.getRawLocation()
. That returns an IPath
, which also has a makeAbsolute()
method if you want to be doubly sure you've got an absolute path.
看起来你想要IResource.getRawLocation()。返回一个IPath,如果你想要确定你有一个绝对路径,它还有一个makeAbsolute()方法。
#2
5
I think a more Java friendly solution would be to do use the following:
我认为更友好的Java解决方案是使用以下方法:
IResource.getLocation().toFile()
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.
这利用了IPath API(getLocation()部分)并将返回一个java.io.File实例。当然,其他答案可能会让你到达你想要的地方。
On a tangential note, I find the IDE class (org.eclipse.ui.ide.IDE)
a useful utility resource when it comes to editors.
在切线上,我发现IDE类(org.eclipse.ui.ide.IDE)在编辑器方面是一个有用的实用程序资源。
#3
4
The answer that worked for me (and I tested it!) was:
对我有用的答案(我测试了它!)是:
// 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
I usually call IFile.getLocation() which returns an IPath and then call IPath.toOSString().
我通常调用IFile.getLocation(),它返回一个IPath,然后调用IPath.toOSString()。
file.getLocation().toOSString()
#5
0
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
For me, this run ok.
对我来说,这运行正常。
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace()。getRoot();
File file = workSpaceRoot.getRawLocation().makeAbsolute().toFile();
File file = workSpaceRoot.getRawLocation()。makeAbsolute()。toFile();
file list from this location:
此位置的文件列表:
File[] files = file.listFiles();
File [] files = file.listFiles();
#1
20
Looks like you want IResource.getRawLocation()
. That returns an IPath
, which also has a makeAbsolute()
method if you want to be doubly sure you've got an absolute path.
看起来你想要IResource.getRawLocation()。返回一个IPath,如果你想要确定你有一个绝对路径,它还有一个makeAbsolute()方法。
#2
5
I think a more Java friendly solution would be to do use the following:
我认为更友好的Java解决方案是使用以下方法:
IResource.getLocation().toFile()
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.
这利用了IPath API(getLocation()部分)并将返回一个java.io.File实例。当然,其他答案可能会让你到达你想要的地方。
On a tangential note, I find the IDE class (org.eclipse.ui.ide.IDE)
a useful utility resource when it comes to editors.
在切线上,我发现IDE类(org.eclipse.ui.ide.IDE)在编辑器方面是一个有用的实用程序资源。
#3
4
The answer that worked for me (and I tested it!) was:
对我有用的答案(我测试了它!)是:
// 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
I usually call IFile.getLocation() which returns an IPath and then call IPath.toOSString().
我通常调用IFile.getLocation(),它返回一个IPath,然后调用IPath.toOSString()。
file.getLocation().toOSString()
#5
0
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
For me, this run ok.
对我来说,这运行正常。
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace()。getRoot();
File file = workSpaceRoot.getRawLocation().makeAbsolute().toFile();
File file = workSpaceRoot.getRawLocation()。makeAbsolute()。toFile();
file list from this location:
此位置的文件列表:
File[] files = file.listFiles();
File [] files = file.listFiles();