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 badges2 Answers
Reset to default 3I'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条)