I am developing an eclipse plugin and I am trying to get the workspace root in order to acces later a file from the workspace and read something from it.
I skipped the error handling for a shorter code
IProject project = file.getProject(); // file is the file open in editor
IFolder specificFolder = project.getFolder("test");
IFile fileFromSpecFolder = specificFolder.getFile("test.txt");
Path path = Paths.get(fileFromSpecFolder.getLocationURI());
BufferedReader reader = createReaderFor(path);
// later on read something from the file...
The problem is that the implemnted getProject
method returns itself for projects or null for the project root
.
public IProject getProject() {
return workspace.getRoot().getProject(path.segment(0));
}
path.segment(0))
contains the workspace root
Am I over complicating things here? How could I achieve this in another way?