1267

Question:
I am trying to write atleast 10 lines to a file using loops to append multiple lines in a file using PhoneGap, but everytime I run this code below, only able to add 1 line to a file as a newline. But 10 lines are not being printed in 10 iteration. Please suggest me how to rectify my problem using phoneGap javascript.
function gotFileWriter(writer) {
for (var z = 0; z<= 10; z++) {
console.log("normal write success");
writer.onwrite = function(evt) {
console.log("write success");
};
writer.onwriteend = function(evt) {
console.log("write end");
}
writer.seek(writer.length);
writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n");
writer.abort(); // I tried even commenting
}
}
Answer1:Have you try this ?
function gotFileWriter(writer) {
console.log("normal write success");
writer.onwrite = function(evt) {
console.log("write success");
};
writer.onwriteend = function(evt) {
console.log("write end");
}
for (var z = 0; z< 10; z++) {
writer.seek(writer.length);
writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n");
writer.abort(); // I tried even commenting
}
}
Answer2:This did the trick for me
Build one big string:
function gotFileWriter(writer)
{
var bigString = '';
for (var i=0; i<numberOfLines; i++)
{
bigString += line[i];
bigString += '\r\n';
}
...
Then do a massive write to the file:
...
writer.onwrite = function (evt) { ... };
writer.onwriteend = function (evt) { ... };
writer.seek(writer.length);
writer.write (bigString);
}
Answer3:You can use this for appending the text to file instead of looping
function gotFileWriter(writer) {
writer.seek(writer.length);
writer.write(" - append- \n");
}