Create Virtual File from non existing file will become null <solved>

Hi,

I'm realy new @java - and so I'm new at plugin-develop.

I want to create a VirtualFile - but there is a problem, that I can't solve, when the file does not exists.

 

I do the following:

private VirtualFile buildVirtualFile() {

Boolean success;
String filePath = getFilePath();
File file = new File(filePath);

if (!file.exists()) {
try {
success = file.createNewFile();
} catch (IOException e) {
success = false;
}
} else {
success = true;
}

VirtualFile fileVirtual = LocalFileSystem.getInstance().findFileByPath(file.getAbsolutePath());

if (fileVirtual == null) {
Messages.showMessageDialog(project, "Virtual File = null", "Information", Messages.getInformationIcon());
}

return fileVirtual;
}

 

There is an action, that will run this buildVirtualFile()

If the file exists, there is no Problem. But if the file doesn't exist, when the methode is called, fileVirtual will always be null.

The success = file.createNewFile(); creates the missing file (success is true, I can see the file in explorer), but nevertheless fileVirtual will be null.

 

So what can I do?

cu

 

 

 

2 comments

Hi,

I think this is the solution:

 

fileVirtual = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
0

VFS is a snapshot of an underlying file system, not a proxy layer on top of it. That means that it may be outdated and one should perform a "refresh" to update the VFS.

In your case, `refreshAndFind*()` is the right solution.

0

Please sign in to leave a comment.