Skip to content

Instantly share code, notes, and snippets.

@mikebranstein
Created January 20, 2016 00:34
Show Gist options
  • Save mikebranstein/d50aa52178ed4c621eac to your computer and use it in GitHub Desktop.
Save mikebranstein/d50aa52178ed4c621eac to your computer and use it in GitHub Desktop.
NativeScript file-system module example
var fileSystemModule = require("file-system");
var fileName = "persistedFile.json";
var file = fileSystemModule.knownFolders.documents().getFile(fileName);
var data = [{"id": "1", "value": "NativeScript"}, ...];
// write data to the file, converted to a JSON string first
file.writeText(JSON.stringify(data));
// read data from the file
file.readText().then(function(content) {
// content contains the data read from the file
});
// clear data form the file
file.clear();
@Jazarro
Copy link

Jazarro commented Oct 19, 2016

In the latest version of NativeScript (2.3.0), clear() is NOT part of the File API. It is a function in sibling class Folder; however, that's not what you want because it deletes all files inside that folder.

Both File and Folder extend class FileSystemEntity, which exposes function "remove(): Promise;" This remove function deletes the file, so that's probably what you want instead of clear().

I've forked your gist and made the necessary changes, but it turns out GitHub doesn't support pull requests for gists.

@elmissouri16
Copy link

readText() is not working is only give empty string you to override it with writeText() first then read the file the problem is if you have file already stored in device how you suppose to override it if you don't have access to his content that's not make any sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment