如何从 Eclipse 中的文件名获取文件 IFile?

How to get file IFile from file name in eclipse?

文件名为字符串。例如:Sample.c。 如何从中获取 IFile?

如果您的路径是文件系统中的完整路径,您可以使用:

IPath path = new Path("C:\Users\runtime-EclipseApplication\Rule_Validate\Sample_Test.h");

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

IFile file = root.getFileForLocation(path);

注意:如果该位置不在工作区中,结果可能是 null

如果您有相对于工作区的路径或工作区中的 project/folder,还有其他 API。