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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With