How do you append to the file instead of overwriting it?
The file-system module documentation explains how to write to a file:
The following example writes some text to a file. It will create a new file or overwrite an existing file.
var documents = fs.knownFolders.documents();
var file = documents.getFile("Test_Write.txt");
// Writing text to the file.
file.writeText("Something")
.then(function () {
// Succeeded writing to the file.
}, function (error) {
// Failed to write to the file.
});
My end goal is to write log files to disk as the nativescript app runs. It'd be too inefficient to read the contents of the log file into memory, append the new log message, and then write it all back to the file.
UPDATE: Ended up logging to an sqlite database. This has some benefits and drawbacks, but for other's trying to solve a similar issue it's another direction to try.
NativeScript currently doesn't provide a appendFile function for its file system module. Reference: https://github.com/NativeScript/NativeScript/issues/4462 A sample implementation of appendFile (text only) can be found here: https://github.com/EddyVerbruggen/nativescript-node/blob/dc93ab48a84c410bc8d6f46527fb171799f8bfeb/fs.js#L233-L254
It is using the "read and then write" method as you said.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With