Unable to append to file using FileWriter

3 posts, 1 answers
  1. Darren
    Darren avatar
    14 posts
    Member since:
    Feb 2009

    Posted 20 Nov 2015 Link to this post

    We are building an iOS app that uses the FileWriter to write to a text file in temporary storage. The code to write to the file is taken from the File System sample app here: http://docs.telerik.com/platform/samples/sample-file/

    Currently, the app is able to write to the file with the necessary data. However, when we restart the app and try to write to the file again it overwrites the previous file rather than appending. 

     

    var fsHelper = new FileSystemHelper();
                fsHelper.writeLine(
                    "offline.txt",
                    "test",
                    success,
                    error
                );

                function success(msg) {
                    console.log(msg);
                }

                function error(msg) {
                    console.log(msg);
                }

     

    The code that we are using for the FileSystemHelper can be found here: https://github.com/Icenium/sample-file/blob/master/sample-file/scripts/fileHelper.js

    We are testing the code within the AppBuilder Device Simulator.

  2. Answer
    Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 25 Nov 2015 Link to this post

    Hello Darren,

    To avoid overwriting the text, the sample uses the seek() method, which moves the write position to the end of the file. I tested this on a device and it works as expected—the new text gets appended to the old.

    If you are restarting the simulator, or using the Reload button, note that the app directory is re-created, so you are actually not writing to the old file but creating a new one. I advise you to test your code on a physical device to confirm if it works as expected.

    Regards,
    Tsvetina
    Telerik
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
  3. Darren
    Darren avatar
    14 posts
    Member since:
    Feb 2009

    Posted 25 Nov 2015 in reply to Tsvetina Link to this post

    Hi Tsvetina,

    I had not tried testing the app on the device itself, so my problem must have been that it was recreating the app directory each time. Thank you!

Back to Top