Get file from path to have a virtual file



Hi would like to have a virtual file from a custom file in my plugin project:

File file = new File("/templates/query.php");
MyPluginProjectComponent.getLogger().info(file.exists() ? "true" : "false");
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);

The file is located in resources folder. How to load a file from a path in the plugin?

The objective here, is to have a virtual file or a psi file to write on the disk the query.php file from my action in "NewGroup" group-id


Or may be it's better to create a file on disk programmatically with PSI? But I don't find any plugin or simple example to do that.


See com.intellij.util.ResourceUtil util class


Thank you for the reply.


I think it will be better if I create a PHP file with PSI.

It's possible to create a virtual file from a PSI file?

For example I would like to create a php file with PSI:

- add an opening php tag

- doc block

- class declaration

- constructor

- ...


I can't find any resources for this :/

I check into the PHP plugin, but the plugin create file from a code template and I want to create a file with PSI.

An API exists to create file from PSI?

For example, a PSI to create a constructor, add a whitespace, add a method, properties, ...?




The current code works but in the log file the text is false :/

URL url = ResourceUtil.getResource(getClass().getClassLoader(), "/templates/", "query.php");
File file = new File(url.getFile());
ZendGeneratorProjectComponent.getLogger().info(file.exists() ? "true" : "false");


Why it can be loaded? (The path and the file are correct in the jar file)


See com.intellij.openapi.vfs.VfsUtil#findFileByURL


Yes, it's working. Thank you ;)


URL url = ResourceUtil.getResource(getClass(), "templates", "query.php");
VirtualFile virtualFile = VfsUtil.findFileByURL(url);
PsiFile file = PsiManager.getInstance(e.getProject()).findFile(virtualFile);

Please sign in to leave a comment.