简体   繁体   中英

Convert IFile to File

I have a IFile object which need to use as java.io.File object. I am converting using following code.

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

But the call to file.exists() returns false.

You want to use IResource.getLocation() which returns the local file path. 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).

IFile is a part of query to modificate workspace, not actual file. IPath is also part of query which is valid on Workspace.

An IFile may not be contained by Workspace physically, eg. Clonned Git Project. An some IFile may be a just link, not actual file. And some IFile may be actually exists on remote location.

Eclipse uses independent File System called "EFS". EFS manages files on workspace and native file system. You can locate local file with 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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