Sunday, September 19, 2010

Easy File Reading, Writing and Appending In NodeJS

Recently, i was having trouble in reading, writing and appending file in NodeJS. I am just a beginner in NodeJS so i go wandering around in google searching for help. In the end, I ended up reading the NodeJS documentation and here goes.

File Reading

Firstly, what you need to do is create a new file called "file.js" anywhere you want which in my case is "/home/pyrostrex/file.js"

Secondly, you need to create a dummy file and name it "test.txt" in the same folder where your "file.js" is. Just add in any content would be good for this tutorial.

After that, you must require the filesystem.

var fs = require("fs");

Then, add in inside these lines of code below.

fs.readFile("test.txt', function(err, data){
  console.log(data);
});

That's it! Now you're done. Just run node file.js inside your terminal and you will see the result!

File Writing (Overwrite)

Now that you know how to read file in NodeJS. Let's move on shall we?

For this writing process, it will overwrite the file. Just like file reading, you must first include the filesystem into our NodeJS code.

var fs = require("fs");

Then, instead of using fs.readFile, you have to use fs.writeFile.

fs.writeFile("test.txt', "This is a hello inside a file!", function(err){
  if (err) console.log(err);
});

Basically, what the above code do is write the entire buffer one time into the file "test.txt"

So that's it for file writing! Easy huh!

File Writing (Append)

You have already know how to read and write file. But you must be wondering, how can i append to a file?

This is still easy but require more lines than before. So here goes.

fs.open("test.txt", 'a', 0666, function(err, fd){

});

What the code above do is open a file called "test.txt" with an 'a' attribute which is the append attribute with the mode '0666'. If you see above, the fd refers to the file handle that we currently opened.

Inside the { ... } tag, you have to put this code.

fs.write(fd, "I'm an appended Hello World!\n", null, undefined, function (err, written) {
  console.log('bytes written: ' + written);
});

There are five arguments here and all the information about what each do is listed below
  • 1st Argument is the file handler that was parsed before.
  • 2nd Argument is the data we wanna append inside the file which in my case "I'm an appended Hello World!" and a newline.
  • 3rd Argument is the position of cursor. In my case i used null because i want it to be in the default position which is at the end of the file.
  • 4th Argument is the encoding. It supports 2 type 'utf8' and 'binary'. But for the sake of this example, I am using the default value which is "undefined". Some people said that i cannot use null but I don't know this because i never tried it before.
  • 5th Argument is the callback. What you wanna do after the file has been append. The 1st argument of the callback is the error argument which will have its value when any error occurred in the process. The 2nd argument is how much data has been written inside. ex. 5 bytes, 10 bytes depending on the length of your data buffer.
So, that's it folks! I hope you found my tutorial today easy to use and easy to understand. Sorry for typos and bad grammar.

3 comments:

  1. This is helpful, thanks! I think you forgot the closing parenthesis and semicolons on some of the code fragments, fyi.

    ReplyDelete
  2. Owh thank you so much for that. I've fixed the code :).

    ReplyDelete
  3. Thank but, the function of writting, does not pass at the new line

    ReplyDelete