简体   繁体   English

将IFile转换为File

[英]Convert IFile to File

I have a IFile object which need to use as java.io.File object. 我有一个IFile对象需要用作java.io.File对象。 I am converting using following code. 我正在使用以下代码进行转换。

file = ifile.getFullPath().toFile();

But the call to file.exists() returns false. 但是对file.exists()的调用返回false。

You want to use IResource.getLocation() which returns the local file path. 您想使用IResource.getLocation()返回本地文件路径。 However, read the javadoc comments carefully as you cannot assume that a resource is backed by an actual file (it can be anything depending on how the project is set up). 但是,请仔细阅读javadoc注释,因为您不能假设资源由实际文件支持(它可以是任何东西,具体取决于项目的设置方式)。

IFile is a part of query to modificate workspace, not actual file. IFile是查询到modificate工作空间的一部分,而不是实际文件。 IPath is also part of query which is valid on Workspace. IPath也是在Workspace上有效的查询的一部分。

An IFile may not be contained by Workspace physically, eg. 工作空间可能不包含IFile,例如。 Clonned Git Project. Clonned Git项目。 An some IFile may be a just link, not actual file. 一些IFile可能只是一个链接,而不是实际文件。 And some IFile may be actually exists on remote location. 而一些IFile实际上可能存在于远程位置。

Eclipse uses independent File System called "EFS". Eclipse使用名为“EFS”的独立文件系统。 EFS manages files on workspace and native file system. EFS管理工作空间和本机文件系统上的文件。 You can locate local file with EFS. 您可以使用EFS查找本地文件。 (If it exist in local) (如果它存在于本地)

IFile file = ...;

// gets URI for EFS.
URI uri = file.getLocationURI();

// what if file is a link, resolve it.
if(file.isLinked()){
   uri = file.getRawLocationURI();
}

// Gets native File using EFS
File javaFile = EFS.getStore(uri).toLocalFile(0, new NullProgressMonitor());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM