javascript - nodejs read file, write to temporary file then copy and replace the original file - Stack Overflow

I am trying to read the file, then create a temporary file then copy and replace the temporary file to

I am trying to read the file, then create a temporary file then copy and replace the temporary file to the original file at the end.

Is it possible to do the following?

// read the original file then save it to temporary file
fs.readFile(originalPath, function (err, data) {
            var json = JSON.parse(data);
            json.items.push("sample item");
            fs.writeFileSync(tmpPath, JSON.stringify(json))
})

// copy the temporary file to the original file path
fs.readFile(tmpPath, (err, contents) => {
            var json = JSON.parse(contents);
            fs.writeFileSync(originalPath, JSON.stringify(json));
});

I am doing this to prevent the changes made to original file until the process has been pleted.

It doesn't seem to work due the the file is not physically saved when reading it.

Thanks.

I am trying to read the file, then create a temporary file then copy and replace the temporary file to the original file at the end.

Is it possible to do the following?

// read the original file then save it to temporary file
fs.readFile(originalPath, function (err, data) {
            var json = JSON.parse(data);
            json.items.push("sample item");
            fs.writeFileSync(tmpPath, JSON.stringify(json))
})

// copy the temporary file to the original file path
fs.readFile(tmpPath, (err, contents) => {
            var json = JSON.parse(contents);
            fs.writeFileSync(originalPath, JSON.stringify(json));
});

I am doing this to prevent the changes made to original file until the process has been pleted.

It doesn't seem to work due the the file is not physically saved when reading it.

Thanks.

Share Improve this question asked Mar 18, 2018 at 23:42 RalphRalph 2,1033 gold badges16 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I'm not sure I followed all that you were trying to do from that short code snippet, but it's possible that you could get into some trouble mixing asynchronous file reading and synchronous file writing like that.

If those two line ran one after the other as you have them written, the second readFile wouldn't see the output created by the writeFileSync that's above it -- the tmpPath file wouldn't have been written to yet.

The copy/edit/replace might be easier to reason with async/await promises that flow like normal code than with callbacks.

fs provides a promise API:

const fsp = require('fs').promises

Then the code can flow from top to bottom by using await for any asynchronous tasks.

// read the original file then save it to temporary file
async function modifyFile(originalPath, tmpPath, item){
  const data = await fsp.readFile(originalPath)

  const json = JSON.parse(data)
  json.items.push(item)

  // Save it
  await fsp.writeFile(tmpPath, JSON.stringify(json))
  await fsp.renameFile(tmpPath, originalPath)
}

modifyFile('orig.json', 'orig.json.tmp', 'new item').catch(console.error)

Note that async/await are ES2017 and require Node.js 7.6+ or a piler like Babel.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744253970a4565304.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信