Read file from project

Answered

Hi,

I am trying to read a file from a project. All I have as an entrypoint is a `PsiFile` instance. I tried `VfsUtil`, `LocalFileSystem` but always get `null` values. Any suggestion to get a file in a project from a `PsiFile` instance?

 

Thanks,

0
3 comments

Julien, please check the How do I get a virtual file? section from our SDK Docs.

Having the VirtualFile instance, you can use 

VfsUtil.loadText(VirtualFile)

to read its content.

0

Thank you for your response. I read that documentation and the problem is that I do not have a method to get the project path. All I have is a reference to "a file" (anywhere in the project) and all I want is to read the /package.json at the root of the project. I believe that all the methods from the mentioned documentation assume that I already have the path of the project, which I do not have. What I want is to be able to open the "package.json" at the root of the project when I am processing another file of the repository.

0

The only thing I can do is getting the profile by using psiFile.getProject().getBasePath() and add the path to this String. Like doing

psiFile.getProject().getBasePath() + "/package.json"

However, that is not using the VFS function, is machine-dependent and will likely fail on some platforms (depending on how they store their code, what machine they use, etc.).

What would be the best way to do this and open a file at the root of the project using VFS-related functions?

 

Thanks,

0

Please sign in to leave a comment.