对于项目外的文件,如何在java中将File转换为IFile?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/960746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:36:18  来源:igfitidea点击:

How to convert from File to IFile in java, for files outside the project?

javaeclipseworkspace

提问by Jenny Smith

Normally, this code is used for converting from Fileto IFile:

通常,此代码用于从Fileto转换IFile

IWorkspace workspace= ResourcesPlugin.getWorkspace();    
IPath location= Path.fromOSString(file.getAbsolutePath()); 
IFile ifile= workspace.getRoot().getFileForLocation(location);

But this only works for java files within the project. How can I get the IFilewhen Filefile is not in the workspace (not in the project)?

但这仅适用于项目中的 java 文件。我怎样才能获得IFileFile文件不是在工作区(不在项目)?

采纳答案by Jenny Smith

I needed this for using the Binding Resolutions from the Eclipse AST framework. But apparently, this is not possible - for anyone who need it, check this site:

我需要这个来使用 Eclipse AST 框架中的绑定分辨率。但显然,这是不可能的 - 对于需要它的任何人,请查看此站点:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

https://bugs.eclipse.org/bugs/show_bug.cgi?id=206391

回答by Zsolt T?r?k

IResourceand its child IFilewere originally meant to be used only for resources located in the workspace. See the Javadoc for IResource.

IResource并且它的子IFile项最初仅用于位于工作区中的资源。请参阅IResourceJavadoc

However, since Eclipse 3.3 there is support for external files as well, see this Eclipse Wiki page.

但是,由于 Eclipse 3.3 也支持外部文件,请参阅此 Eclipse Wiki 页面