IWorkspaceRoot.getFileForLocation() returns null
I want to obtain IFile handler for a file in one of my projects.
I have a Java project "test" with "x.dioc" file inside. When I look in my local file system and check the file's path it's:
C:\Users\Pawel\runtime-New_configuration\test\src\x.diocNow, I try to get handler:
IPath p = new Path("file:/C:/Users/Pawel/runtime-New_configuration/test/src/x.dioc");
IFile sourceFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(p);
but I get null
.
I've read that this method returns null if the file is not a part of the workspace. But when I开发者_开发知识库 browse "test" project in package explorer I can see "x.dioc" file there.
What might be the cause?
Remove the "file:" at the beginning of your Path variable. Simply create the path like this:
IPath p = new Path("C:/Users/Pawel/runtime-New_configuration/test/src/x.dioc");
Then your IFile sourceFile should contain the correct reference.
I used getFile instead of getFileForLocation and it worked. Thanks for the help! :-)
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IFile resultFile = root.getFile(iPath);
I guess the file path is not correct. Why do u need to specify the complete file path when you are getting the root of the workspace programmaticaly ? You can try using any of the following strings for the file path.
IPath p = new Path("/test/src/x.dioc");
IFile sourceFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(p);
Kindly let me know if this works.
This works:
IPath p = new Path("/test/src/x.dioc");
IFile sourceFile = ResourcesPlugin.getWorkspace().getRoot().getFile(p);
But getFileForLocation did not (at least for me).
精彩评论