Hi Peng

Ideally you'd want to avoid using the Sync versions of these functions as 
they block the system. Here is an example of how you can write to the file 
after a read has completed. By passing a function as a callback to be run 
when the function has completed.

var fs = require("fs");

console.log("starting read...")

fs.readFile("./test.txt", (err,data)=>{
    if (err) {console.log(err); return;}
    console.log("read is completed.")
    console.log(data.toString())

    var newdata = "some new data timestamp:"+new Date()
    console.log("starting write...")
    fs.writeFile("./test.txt", newdata, (err)=>{
        if (err) {console.log(err); return;}
        console.log("write success")
    })
})

On Wednesday, 23 October 2019 17:45:02 UTC+2, Peng Yu wrote:
>
> I'd like to fs.writeFileSync() to the same file after reading its content 
> by fs.readFileSync(). It is not clear to me whether fs.closeSync() (or any 
> other file closing function) is needed between writeFileSync() and 
> readFileSync() according to the documentation. Would you please let me 
> know? Thanks.
>
> https://nodejs.org/api/fs.html
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/6a6d6486-aa71-402f-b138-a1fa2bf170bc%40googlegroups.com.

Reply via email to