I used a filewriter in chrome packaged apps to save a json string. I followed the samples where the writer is set to truncate at the length of the blob, so files can be overwritten with shorter data.
this.writeLocalFile = function(){
chrome.fileSystem.getWritableEntry(self.localFile, function(writableEntry){
console.log(writableEntry);
writableEntry.createWriter(function(writer) {
writer.onerror = function(){
console.log("error");
}
writer.onwriteend = function(){
console.log("Write ended");
}
self.getData();
blob = new Blob([self.data], {type: 'text/plain'});
writer.truncate(blob.size);
//writer.seek(0);
writer.write(blob);
}, function(error){
console.log(error);
});
})
}
the issue is that when I overwrite a longer file with shorter data, if i dont use truncate, the extra data is left and i cant read in a valid json on next load, if i do use truncate, all that extra space is weird null charcters when i look in sublime
So, the read string is still the length of the old longer file, but when i log it in console, the string is correct, and i can copy and paste the output and set it to another variable for the shorter length. But the object read directly is still the longer length and I cant figure out how to trim the null characters from the end
For example, if the file was 1200 chars, and I delete some data and resave so the json string is 800 chars, the last 400 chars in the file are now null. When I reread the data next load, the string is still 1200 chars, with the last 400 being ""
weirdstring[900]//""
weirdstring[900] == "" // false
weirdstring[900] == null // false
weirdstring[900].length // 1
!weirdstring[900] // false
weirdstring.charCodeAt(900) // 0
is there a better to way to fix this than scan the string, check for charCodeAt==0 and removing the rest? Like, a better way to write to the file?