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

发布于 2024-07-10 00:57:27 字数 356 浏览 10 评论 0原文

我想编写一个插件来对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浪菊怪哟 2024-07-17 00:57:27

看起来你想要 IResource.getRawLocation()。 它会返回一个 IPath,如果您想双重确定自己拥有绝对路径,它还有一个 makeAbsolute() 方法。

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.

挥剑断情 2024-07-17 00:57:27

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

IResource.getLocation().toFile()

这利用了 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:

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.

On a tangential note, I find the IDE class (org.eclipse.ui.ide.IDE) a useful utility resource when it comes to editors.

九八野马 2024-07-17 00:57:27

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

// 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);

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);
手心的温暖 2024-07-17 00:57:27

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

file.getLocation().toOSString()

I usually call IFile.getLocation() which returns an IPath and then call IPath.toOSString().

file.getLocation().toOSString()
私野 2024-07-17 00:57:27
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
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
深空失忆 2024-07-17 00:57:27

对我来说,这运行正常。

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文