Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file contents from VirtualFile - intellij plugin development

How can I read file contents from a virtual file. I am currently using this way

BufferedReader br = new BufferedReader(new InputStreamReader(virtualFile.getInputStream()));
            String currentLine;
            StringBuilder stringBuilder = new StringBuilder();
            while ((currentLine = br.readLine()) != null) {
                stringBuilder.append(currentLine);
                stringBuilder.append("\n");
            }
        } catch (IOException e1) {
            e1.printStackTrace();
            return 0;
        }

However am getting some garbled string appended when I print the stringbuilder.

like image 640
Bijesh Avatar asked Oct 12 '25 18:10

Bijesh


1 Answers

Some common ways of reading VirtualFile contents are:

  • file.contentsToByteArray()
  • LoadTextUtil.loadText(file)
  • FileDocumentManager.getInstance().getDocument(file).get*CharSequence()
like image 83
Peter Gromov Avatar answered Oct 15 '25 19:10

Peter Gromov