FileChooser gives same file contents the second time I open the same (updated) file
I'm having an issue with the plugin I'm writing when I attempt to use FileChooser.choosefile() to select the same file (after editing it) a second time. For example, using the code below, if I select a text file with the contents "hello" it will print "hello" to stderr. But if I edit the file to say "hello world" and reopen it with the FileChooser, it still prints "hello". Any ideas?
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
Project project = e.getData(DataKeys.PROJECT);
VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), project, null);
if(file != null) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()));
String line;
// read in lines
while ((line = br.readLine()) != null) {
System.err.println(line);
}
br.close();
} catch (Exception e1) {
PolicyViolationAppComponent.logger.error("Error parsing models");
e1.printStackTrace();
}
}
Please sign in to leave a comment.
In IDEA, VFS is a caching snapshot.
Most probably, the file you're opening doesn't belong to any project loaded into the IDE, so its state isn't updated automatically. Try to call `VirtualFile#refresh` on it.
Perfect! That did the trick.
Thanks.