6

I have an array of data which i want to loop over and while looping over I want to write them to the same file. How i can achieve the same, my following code will only print the last iteration.

for (j = 0; j < arrayPart.length; j++){
    fs.writeFileSync('message.txt', arrayPart[j])
}

message.txt will have the last value of arrayPart.

1
  • writeFile overwrites the file every time you call it. See the docs, specifically "writes data to a file, replacing the file if it already exists." Commented Oct 15, 2018 at 20:56

2 Answers 2

4

Instead of opening/writing/closing at every iteration I would open a write stream, write inside the loop and close at the end:

 const message = fs.createWriteStream(__dirname + "./message.txt");
 for (let j = 0; j <arrayPart.length; j++){
      message.write(arrayPart[j]);
 }
 message.close();

Or you just join the array and write all at once:

 fs.writeFileSync('message.txt', arrayPart.join(""));
Sign up to request clarification or add additional context in comments.

3 Comments

Yes it worked!, now i have one more question, the arrayPart array has a size of 600bytes. but i need only 200 bytes of it, How can i mention the size , while looping that it should just write 200 bytes.
@nidhi whats inside the array?
arrayPart is an array of responses which we get from hitting couple of URL's, I have stored all the responses in an array(name is arrayPart). Now i am looping over arrayPart and writing it to file. Since I want the file to be particular size. also can i use the same thing if file is not '.txt' but '.jpg'
2

Just join the array and write to the file once:

fs.writeFileSync('message.txt', arrayPart.join(""));

8 Comments

Yes it worked!, now i have one more question, the arrayPart array has a size of 600bytes. but i need only 200 bytes of it, How can i mention the size , while looping that it should just write 200 bytes
@NidhiSharma arrayPart.join("") will result in a string. If you want to write only the first 200 character of that string, you could slice it like fs.writeFileSync('message.txt', arrayPart.join("").slice(0, 200));
@NidhiSharma in case the string contains some multi-byte characters, and you want exactly 200 bytes, then just transform it into a buffer first then slice that buffer: fs.writeFileSync('message.txt', Buffer.from(arrayPart.join(""), "utf8").slice(0, 200));
Ok, got it. But arrayPart is an array of responses that we get from couple of URL's, In some cases we might want to write the image (like sample.jpg). will this help me in writing it to image files also
@NidhiSharma I'm not sure. If the array contains buffers instead of strings, then join won't work as expected, you might want to use Buffer.concat for that: fs.writeFileSync('sample.jpg', Buffer.concat(arrayPart)). Buffer.concat returns a buffer, so you can use slice on it like before.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.