Is file closing explicitly needed after fs.readFileSync()?

4,410 views
Skip to first unread message

Peng Yu

unread,
Oct 23, 2019, 11:45:02 AM10/23/19
to nodejs
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.

Rouan van der Ende

unread,
Oct 24, 2019, 11:27:12 AM10/24/19
to nodejs
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")
    })
})

Peng Yu

unread,
Oct 24, 2019, 12:04:02 PM10/24/19
to nod...@googlegroups.com
File reading and writing are not the bottleneck of my program. I don't
want to use the nonblocking version. Plus, the nonblocking version of
the code make the code nested which causes readability problems.

Could you answer my original question? Thanks.
> --
> 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 a topic in the Google Groups "nodejs" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/Xr9qZEqNuw8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/6a6d6486-aa71-402f-b138-a1fa2bf170bc%40googlegroups.com.



--
Regards,
Peng

Chethiya Abeysinghe

unread,
Oct 28, 2019, 11:40:33 AM10/28/19
to nodejs
Hi


On Wednesday, October 23, 2019 at 9:15:02 PM UTC+5:30, 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.


No. These two function are quite high level function that do not involve file descriptors, you are just passing a path. So I guess file opening and closing is handled underneath nodejs. I have used these two function extensively when starting up the the programs that runs in production, to read/write config files. Had no problems so far. 

Lukas Wilkeer

unread,
Nov 1, 2019, 6:08:09 PM11/1/19
to nodejs


Em quarta-feira, 23 de outubro de 2019 12:45:02 UTC-3, Peng Yu escreveu:
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.


No, you don't need to close or other method since they are synchronous functions.

 
Reply all
Reply to author
Forward
0 new messages