Convert IFile to File

I have an IFile object that needs to be used as a java.io.File object. I convert using the following code.

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

But calling file.exists() returns false.

+6
source share
2 answers

You want to use IResource.getLocation() , which returns the path to the local file. However, read the javadoc comments carefully, as you cannot assume that the resource is supported by the actual file (it can be any, depending on how the project is configured).

+9
source

IFile is part of the request to modify the workspace, not the actual file. IPath is also part of the request, which is valid in the workspace.

An IFile may not be physically contained in the workspace, for example. Git cloning project. Some IFile may be just a link, not an actual file. And some IFile may exist in a remote place.

Eclipse uses an independent file system called EFS. EFS manages files on the workspace and in its own file system. You can find the local file with EFS. (If it exists locally)

 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()); 
+3
source

Source: https://habr.com/ru/post/905672/


All Articles