This is a migrated thread and some comments may be shown as answers.

Unable to append to file using FileWriter

2 Answers 113 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Darren
Top achievements
Rank 1
Darren asked on 20 Nov 2015, 03:59 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Tsvetina
Telerik team
answered on 25 Nov 2015, 09:03 AM
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.

 
0
Darren
Top achievements
Rank 1
answered on 25 Nov 2015, 09:10 AM

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!

Tags
Apache Cordova
Asked by
Darren
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Darren
Top achievements
Rank 1
Share this question
or