Reading and Writing Files With NodeJS

Reading From Files

Being capable of reading from files to your local record machine may be hugely useful and there are some unique things you could construct on top of this. A blog reader, importing information from spreadsheets and xml documents or whatever you can think of, being able to study from documents is hugely useful.

The FS Package

Create a new document known as app.Js and add the following:

app.js

var fs = require("fs");

fs.readFile("temp.txt", function(err, buf) {
console.log(buf.toString());
});

Create a temp.Txt within the same directory and write in it whatever you’d like. Run your script the use of node app.Js and you should see in the console the contents of your document. Learn more for Nodejs Online Training

Understanding the Code

We’ll step via this with comments.

var fs = require("fs");

This line does the job of importing the fs package and allowing us to utilize it within our own code.

fs.readFile("temp.txt", function(err, buf) {
console.log(buf);
});

This calls the readFile function asynchronously and then prints the contents of the file to the console.

Handling Errors

If you want to catch errors which includes the file you are attempting to reach isn’t determined, then you could do so like this:

fs.readFile("not-found.txt", "utf-8", (err, data) => {
if (err) { console.log(err) }
console.log(data);
})

When you go to execute this, you should see something like this returned:

$ node app.js

{ Error: ENOENT: no such file or directory, open 'not-found.txt'
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'not-found.txt' }
undefined

Returning a Buffer?

If the above code hasn’t labored as expected and you're seeing a buffer being revealed out in the terminal then it is probably an idea to specify the documents encoding. We can try this like so:

var fs = require("fs");

fs.readFile("temp.txt", "utf-8", (err, data) => {
console.log(data);
});

Writing To Files

Now that you’ve were given the reading of documents down, it’s time to start modifying these files. To try this we’ll be the use of the equal FS package we used in element one.

The Code:

Again create a new file inside your modern-day listing and speak to it write.Js and then add the following javascript code:

write.Js

var fs = require("fs");

var data = "New File Contents";

fs.writeFile("temp.txt", data, (err) => {
if (err) console.log(err);
console.log("Successfully Written to File.");
});

Run this code by using executing node write.Js in the terminal and then open up temp.Txt on your editor, you have to now see the brand new contents of the report.

Creating New Files - The above code will correctly create new files for you should the course to the record no longer already exist. This is handy because it means you may succinctly create, and write to a new file in one promise.

Conclusion

Hopefully, you determined this tutorial beneficial, if you did, or when you have any recommendations or feedback, then please permit me to know inside the comments section below!

Further Reading

If you enjoyed this Post, you could also like a number of our other like Nodejs certification

Top Node JS Tools for 2020

No publications here.