Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IFile to File

Tags:

java

eclipse

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.

like image 211
Viral Avatar asked Jan 11 '12 00:01

Viral


2 Answers

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).

like image 144
Francis Upton IV Avatar answered Nov 15 '22 00:11

Francis Upton IV


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());
like image 35
jeeeyul Avatar answered Nov 14 '22 23:11

jeeeyul