A virtual file com.intellij.openapi.vfs.VirtualFile is the IntelliJ Platform’s representation of a file in a file system (VFS). Most commonly, a virtual file is a file in your local file system. However, the IntelliJ Platform supports multiple pluggable file system implementations, so virtual files can also represent classes in a JAR file, old revisions of files loaded from a version control repository, and so on.
The VFS level deals only with binary content. You can get or set the contents of a
VirtualFile as a stream of bytes, but concepts like encodings and line separators are handled on higher system levels.
- From an action:
e.getData(PlatformDataKeys.VIRTUAL_FILE). If you are interested in multiple selection, you can also use
- From a path in the local file system:
- From a PSI file:
psiFile.getVirtualFile()(may return null if the PSI file exists only in memory)
- From a document:
Typical file operations are available, such as traverse the file system, get file contents, rename, move, or delete. Recursive iteration should be performed using
VfsUtilCore.iterateChildrenRecursively to prevent endless loops caused by recursive symlinks.
The VFS is built incrementally, by scanning the file system up and down starting from the project root. New files appearing in the file system are detected by VFS refreshes. A refresh operation can be initiated programmatically using (
VirtualFile.refresh()). VFS refreshes are also triggered whenever file system watchers receive file system change notifications (available on the Windows and Mac operating systems).
As a plugin developer, you may want to invoke a VFS refresh if you need to access a file that has just been created by an external tool through the IntelliJ Platform APIs.
A particular file on disk is represented by equal
VirtualFile instances for the entire lifetime of the IDEA process. There may be several instances corresponding to the same file, and they can be garbage-collected. The file is a
UserDataHolder, and the user data is shared between those equal instances. If a file is deleted, its corresponding VirtualFile instance becomes invalid (the
isValid() method returns
false and operations cause exceptions).
Usually you don’t. As a rule, files are created either through the PSI API or through the regular java.io.File API.
If you do need to create a file through VFS, you can use the
VirtualFile.createChildData() method to create a
VirtualFile instance and the
VirtualFile.setBinaryContent() method to write some data to the file.
VirtualFileManager.addVirtualFileListener() method allows you to receive notifications about all changes in the VFS.
To provide an alternative file system implementation (for example, an FTP file system), implement the VirtualFileSystem class (most likely you’ll also need to implement
VirtualFile), and register your implementation as an application component.
To hook into operations performed in the local file system (for example, if you are developing a version control system integration that needs custom rename/move handling), implement the LocalFileOperationsHandler interface and register it through the
See IntelliJ Platform Virtual File System for a detailed description of the VFS architecture and usage guidelines.