Read and Write file using vs code extension

user6639252 picture user6639252 · Aug 5, 2016 · Viewed 11.9k times · Source

i am building an extension to parse json using vs code extension. so my need is ,it should be able able to load .json file from a particular folder and iterate through content of the file. Then it should allow user to select few keys from it make a new json file out of this and save it in any folder.

But i am not able to find any way to read and write files in "vs code extension".Could someone please help me.

Answer

Steffen picture Steffen · Aug 11, 2016

If you want to read the current edit state of a file you can use the following API workspace function:

vscode.workspace.openTextDocument(uri).then((document) => {
  let text = document.getText();
});

This will show you the current state of the file including unpersisted changes. document is of type TextDocument and has isDirty set to true if it has pending changes.