对于项目外的文件,如何在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
How to convert from File to IFile in java, for files outside the project?
提问by Jenny Smith
Normally, this code is used for converting from File
to IFile
:
通常,此代码用于从File
to转换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 IFile
when File
file is not in the workspace (not in the project)?
但这仅适用于项目中的 java 文件。我怎样才能获得IFile
当File
文件不是在工作区(不在项目)?
采纳答案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 框架中的绑定分辨率。但显然,这是不可能的 - 对于需要它的任何人,请查看此站点:
回答by Zsolt T?r?k
IResource
and its child IFile
were originally meant to be used only for resources located in the workspace. See the Javadoc for IResource.
IResource
并且它的子IFile
项最初仅用于位于工作区中的资源。请参阅IResource的Javadoc。
However, since Eclipse 3.3 there is support for external files as well, see this Eclipse Wiki page.
但是,由于 Eclipse 3.3 也支持外部文件,请参阅此 Eclipse Wiki 页面。