I’m trying to correctly and efficiently get the number of characters in a file in Nodejs because I want to add new data at a specific position before the end of the file.
I have tried the following:
const fileHandle: fs.FileHandle = await fs.open("someFile", "r+");
let numberOfCharacters = 0;
for await (const line of fileHandle.readLines({autoClose: false, encoding: "utf8"})) {
numberOfCharacters += line.length;
}
This gave me the same value as when I copied the text in the file into Microsoft Word (Word tells you the number of characters in the document).
But when I tried this:
await fileHandle.write(`someText`, numberOfCharacters - 9, "utf8");
It overwrote text that was about 12 characters before the end of the file.
I then tried this:
const numberOfCharacters = (await fileHandle.readFile({encoding: "utf8"})).length;
This gave me a value higher than what I got from the code above, but it worked perfectly well when I did the fileHandle.write(`someText`, numberOfCharacters - 9, "utf8")
.
The problem is that the amount of data that will be written to that file every day is massive and I’m afraid a day might come when there won’t be enough ram on my server to handle reading the entire file await fileHandle.readFile({encoding: "utf8"})
.
Is there a less memory hogging way of getting the number of characters in a file in Nodejs?
Also, can anyone explain why these two methods give different results:
const fileHandle: fs.FileHandle = await fs.open("someFile", "r+");
let numberOfCharacters = 0;
for await (const line of fileHandle.readLines({autoClose: false, encoding: "utf8"})) {
numberOfCharacters += line.length;
}
and
const numberOfCharacters = (await fileHandle.readFile({encoding: "utf8"})).length;