Create file from string and open in Editor
Answered
Hello,
I need to create new file and open it in editor. I tried different approaches, but always failed because VirtualFile is null.
For example, I tried
PsiFile file = psiFileFactory.createFileFromText("readme.md", PlainTextFileType.INSTANCE, "testing");
But in this case file.getVirtualFile() always null.
Also tried Java way
Path path = Path.of(Objects.requireNonNull(project.getBasePath()), "readme.md");
Files.write(path, "test".getBytes());
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path.toString());
But virtualFile always null.
How to properly do this? Could someone help?
Thanks!
Please sign in to leave a comment.
Hi Alexandr,
Please try to add the created file to a directory, according to the documentation:
https://plugins.jetbrains.com/docs/intellij/psi-files.html#how-do-i-create-a-psi-file
To open a file in the editor use com.intellij.openapi.fileEditor.OpenFileDescriptor.