Node.js File System
Node.js File System
js - File System
Node implements File I/O using simple wrappers around standard POSIX functions. The Node File System (fs) module can
be imported using the following syntax −
var fs = require("fs")
Synchronous vs Asynchronous
Every method in the fs module has synchronous as well as asynchronous forms. Asynchronous methods take the last
parameter as the completion function callback and the first parameter of the callback function as error. It is better to use
an asynchronous method instead of a synchronous method, as the former never blocks a program during its execution,
whereas the second one does.
Example
var fs = require("fs");
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
$ node main.js
Program Ended
Asynchronous read: Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
The following sections in this chapter provide a set of good examples on major File I/O methods.
Open a File
Syntax
Parameters
flags − Flags indicate the behavior of the file to be opened. All possible values have been mentioned below.
mode − It sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666,
readable and writeable.
callback − This is the callback function which gets two arguments (err, fd).
Flags
Flags for read/write operations are −
Example
Let us create a js file named main.js having the following code to open a file input.txt for reading and writing.
var fs = require("fs");
$ node main.js
Syntax
Following is the syntax of the method to get the information about a file −
fs.stat(path, callback)
Parameters
callback − This is the callback function which gets two arguments (err, stats) where stats is an object of fs.Stats
type which is printed below in the example.
Apart from the important attributes which are printed below in the example, there are several useful methods available in
fs.Stats class which can be used to check file type. These methods are given in the following table.
Example
var fs = require("fs");
$ node main.js
Writing a File
Syntax
This method will over-write the file if the file already exists. If you want to write into an existing file then you should use
another method available.
Parameters
path − This is the string having the file name including path.
data − This is the String or Buffer to be written into the file.
options − The third parameter is an object which will hold {encoding, mode, flag}. By default. encoding is utf8,
mode is octal value 0666. and flag is 'w'
callback − This is the callback function which gets a single parameter err that returns an error in case of any
writing error.
Example
$ node main.js
Reading a File
Syntax
This method will use file descriptor to read the file. If you want to read the file directly using the file name, then you
should use another method available.
Parameters
buffer − This is the buffer that the data will be written to.
offset − This is the offset in the buffer to start writing at.
position − This is an integer specifying where to begin reading from in the file. If position is null, data will be read
from the current file position.
callback − This is the callback function which gets the three arguments, (err, bytesRead, buffer).
Example
var fs = require("fs");
var buf = new Buffer(1024);
$ node main.js
Closing a File
Syntax
fs.close(fd, callback)
Parameters
Example
var fs = require("fs");
var buf = new Buffer(1024);
Truncate a File
Syntax
Parameters
callback − This is the callback function No arguments other than a possible exception are given to the completion
callback.
Example
var fs = require("fs");
var buf = new Buffer(1024);
$ node main.js
Delete a File
Syntax
fs.unlink(path, callback)
Parameters
Example
var fs = require("fs");
$ node main.js
Create a Directory
Syntax
Parameters
callback − This is the callback function No arguments other than a possible exception are given to the completion
callback.
Example
var fs = require("fs");
$ node main.js
Read a Directory
Syntax
fs.readdir(path, callback)
Parameters
Example
var fs = require("fs");
$ node main.js
Remove a Directory
Syntax
fs.rmdir(path, callback)
Parameters
Example
var fs = require("fs");
fs.readdir("/tmp/",function(err, files) {
if (err) {
return console.error(err);
}
files.forEach( function (file) {
console.log( file );
});
});
});
$ node main.js
Methods Reference
Following is a reference of File System module available in Node.js. For more detail you can refer to the official
documentation.