locked
How to append json string to a file in a new line in javascript

    Question

  • WinJS.Application.local.writeText("json.db", JSON.stringify(myDataSource)).then(

    Hi,

    Is there a way to append the JSON string to the next line in file "json.db"

    The above code overwrites the previous string line.

    Thanks

    Monday, April 30, 2012 4:06 PM

Answers

  • I can't comment on how well your 'json.db' will fare in your scenario but there is a way to append a string to an existing file in the application local store:

            var localFolder = Windows.Storage.ApplicationData.current.localFolder;
            localFolder.getFileAsync("myFile.txt")
            .then(function (file) {
                return Windows.Storage.FileIO.appendTextAsync(file, "more text");
            }).done();  

    You can read the file using the shorter version:

    WinJS.Application.local.readText("myFile.txt", "none found")
            .then(function (text) {...


    Monday, April 30, 2012 5:55 PM