articles

Home / DeveloperSection / Articles / Read/Write file in Node.js

Read/Write file in Node.js

Anchal Kesharwani9446 18-Aug-2014

In this article, I’m explaining the concept of read and write functionality in node.js.  How to manage file system in node.js we will discuss I this article.

Read file

Reading the contents of a file into memory is a very common programming task, and, as with many other things, the Node.js core API provides methods to make this trivial. There are a variety of file system methods, all contained in the fs module. The easiest way to read the entire contents of a file is with filestream.readFile(), as follows:

Syntax

filestream= require('fs'); fs.readFile(file, [encoding], [callback]); //

filestream. = (string) filepath of the file to read 

‘file’ parameter is used for give the filename it is necessary to give else it give the

error.

‘encoding’ is an optional parameter that specifies the type of encoding to read the file. Possible encodings are 'ascii', 'utf8', and 'base64'. If no encoding is provided, the default is utf8.

‘callback’ is a function to call when the file has been read and the contents are ready - it is passed two arguments, error and data. If there is no error, error will be null and data will contain the file contents; otherwise err contains the error message.

We create a simple program to read the file in node.js

filestream = require('fs')

filestream.readFile('/temp/test.txt', 'utf8', function (err,data) {

  if (err) {

    return console.log(err);

  }

  console.log(data);

});

 

Here temp is the folder that keep the test.txt file and test.txt file store some data. The above program print the file data in the console.

Now run your program by the node command with <program file>. Open command prompt with administrator and select the location where your program store. Here we select the location of c:\node;

Read/Write file in Node.js

Write file

Writing to a file is another of the basic programming tasks that one usually needs to know about - luckily, this task is very simple in Node.js. We can use the handy writeFile() method inside the standard library's fs module, which can save all sorts of time and trouble.

Syntax

filestream = require('fs');

filestream.writeFile(filename, data, [encoding], [callback])

 

‘file’ filepath to file write.

‘data’ string or buffer that you want to write in the file.

‘encoding’ optional parameter the encoding of the data. Possible encodings are

'ascii', 'utf8', and 'base64'. If no encoding provided, then 'utf8' is assumed.

‘callback’  function if there is no error callback.

Here we give the very simple example of write file in file system in node.js

filestream = require('fs');

filestream.writeFile('test.txt', 'Hello World!', function (err) {

  if (err) return console.log(err);

  console.log('Hello World!');

});

 

Now run your program by the node command with <program file>. Open

command prompt with administrator and select the location where your program

store. Here we select the location of c:\node;

Read/Write file in Node.js

Hit enter button and now you will see the test file. You will see that something

change into the file:

Read/Write file in Node.js

If file not exist then it give error.

Conclusion

In this blog I’m explaining how to read file in node.js and write the file in node.js.


Updated 07-Sep-2019

Leave Comment

Comments

Liked By