Pure JavaScript is Unicode friendly, but it is not so for binary data. While dealing with TCP streams or the file system, it's necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.
Buffer class is a global class that can be accessed in an application without importing the buffer module.
Creating Buffers
Node Buffer can be constructed in a variety of ways.
Method 1
Following is the syntax to create an uninitiated Buffer of 10 octets −
var buf = Buffer.alloc(15);// Create an empty buffer of size 15. // A buffer that only can accommodate 15 bytes.
Method 2
Following is the syntax to create a Buffer from a given string and optionally encoding type −
var buf = new Buffer.from("simple buffer demonstration", "utf-8");// Create a buffer with content
Though "utf8" is the default encoding, you can use any of the following encodings "ascii", "utf8", "utf16le", "ucs2", "base64" or "hex".
Writing to Buffers
Syntax
Following is the syntax of the method to write into a Node Buffer −
buf.write(string[, offset][, length][, encoding])
Parameters
Here is the description of the parameters used −
- string − This is the string data to be written to buffer.
- offset − This is the index of the buffer to start writing at. Default value is 0.
- length − This is the number of bytes to write. Defaults to buffer.length.
- encoding − Encoding to use. 'utf8' is the default encoding.
Return Value
This method returns the number of octets written. If there is not enough space in the buffer to fit the entire string, it will write a part of the string.
Example
buf = new Buffer.alloc(256); len = buf.write("simple buffer demonstration"); console.log("Octets written : "+ len);
When the above program is executed, it produces the following result −
Octets written : 20
Reading from Buffers
Syntax
Following is the syntax of the method to read data from a Node Buffer −
buf.toString([encoding][, start][, end])
Parameters
Here is the description of the parameters used −
- encoding − Encoding to use. 'utf8' is the default encoding.
- start − Beginning index to start reading, defaults to 0.
- end − End index to end reading, defaults is complete buffer.
Return Value
This method decodes and returns a string from buffer data encoded using the specified character set encoding.
Example
buf = new Buffer.alloc(26); for (var i = 0 ; i 0){ console.log("Here is the content that we have read from file : "); console.log(buf.slice(0, bytes).toString()); } }); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to open an existing file File opened successfully! Going to read the file 25 bytes read Here is the content that we have read from file : This is some sample text!
Closing a File
Syntax
Following is the syntax to close an opened file −
fs.close(fd, callback)
Parameters
Here is the description of the parameters used −
- fd − This is the file descriptor returned by file fs.open() method.
- callback − This is the callback function No arguments other than a possible exception are given to the completion callback.
Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("Going to open an existing file"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("File opened successfully!"); console.log("Going to read the file"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) { if (err) { console.log(err); } // Print only read bytes to avoid junk. if(bytes > 0) { console.log(buf.slice(0, bytes).toString()); } // Close the opened file. fs.close(fd, function(err) { if (err) { console.log(err); } console.log("File closed successfully."); }); }); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to open an existing file File opened successfully! Going to read the file This is some sample text! File closed successfully.
Truncate a File
Syntax
Following is the syntax of the method to truncate an opened file −
fs.ftruncate(fd, len, callback)
Parameters
Here is the description of the parameters used −
- fd − This is the file descriptor returned by fs.open().
- len − This is the length of the file after which the file will be truncated.
- callback − This is the callback function No arguments other than a possible exception are given to the completion callback.
Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("Going to open an existing file"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("File opened successfully!"); console.log("Going to truncate the file after 10 bytes"); // Truncate the opened file. fs.ftruncate(fd, 10, function(err) { if (err) { console.log(err); } console.log("File truncated successfully."); console.log("Going to read the same file"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err) { console.log(err); } // Print only read bytes to avoid junk. if(bytes > 0) { console.log(buf.slice(0, bytes).toString()); } // Close the opened file. fs.close(fd, function(err) { if (err) { console.log(err); } console.log("File closed successfully."); }); }); }); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to open an existing file File opened successfully! Going to truncate the file after 10 bytes File truncated successfully. Going to read the same file This is so File closed successfully.
Delete a File
Syntax
Following is the syntax of the method to delete a file −
fs.unlink(path, callback)
Parameters
Here is the description of the parameters used −
- path − This is the file name including path.
- callback − This is the callback function No arguments other than a possible exception are given to the completion callback.
Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); console.log("Going to delete an existing file"); fs.unlink('input.txt', function(err) { if (err) { return console.error(err); } console.log("File deleted successfully!"); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to delete an existing file File deleted successfully!
Create a Directory
Syntax
Following is the syntax of the method to create a directory −
fs.mkdir(path[, mode], callback)
Parameters
Here is the description of the parameters used −
- path − This is the directory name including path.
- mode − This is the directory permission to be set. Defaults to 0777.
- callback − This is the callback function No arguments other than a possible exception are given to the completion callback.
Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); console.log("Going to create directory /demo/files"); fs.mkdir('demo',function(err) { if (err) { return console.error(err); } console.log("Directory created successfully!"); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to create directory Directory created successfully!
Read a Directory
Syntax
Following is the syntax of the method to read a directory −
fs.readdir(path, callback)
Parameters
Here is the description of the parameters used −
- path − This is the directory name including path.
- callback − This is the callback function which gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.
Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); console.log("Going to read directory "); fs.readdir("demo",function(err, files) { if (err) { return console.error(err); } files.forEach( function (file) { console.log( file ); }); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to read directory file1.html file2.py file3.java file4.txt
Remove a Directory
Syntax
Following is the syntax of the method to remove a directory −
fs.rmdir(path, callback)
Parameters
Here is the description of the parameters used −
- path − This is the directory name including path.
- callback − This is the callback function No arguments other than a possible exception are given to the completion callback.
path − This is the directory name including path.
callback − This is the callback function No arguments other than a possible exception are given to the completion callback.Example
Let us create a js file named app.js having the following code −
var fs = require("fs"); console.log("Going to delete directory tmp"); fs.rmdir("demo/tmp",function(err) { if (err) { return console.error(err); } console.log("Going to read directory "); fs.readdir("demo",function(err, files) { if (err) { return console.error(err); } files.forEach( function (file) { console.log( file ); }); }); });
Now run the app.js to see the result −
$ node app.js
Verify the Output.
Going to delete directory tmp Going to read directory file1.html file2.py file3.java file4.txt
Methods Reference
Sr.No Method & Description 1 fs.rename(oldPath, newPath, callback) Asynchronous rename(). No arguments other than a possible exception are given to the completion callback.
2 fs.ftruncate(fd, len, callback) Asynchronous ftruncate(). No arguments other than a possible exception are given to the completion callback.
3 fs.ftruncateSync(fd, len) Synchronous ftruncate().
4 fs.truncate(path, len, callback) Asynchronous truncate(). No arguments other than a possible exception are given to the completion callback.
5 fs.truncateSync(path, len) Synchronous truncate().
6 fs.chown(path, uid, gid, callback) Asynchronous chown(). No arguments other than a possible exception are given to the completion callback.
7 fs.chownSync(path, uid, gid) Synchronous chown().
8 fs.fchown(fd, uid, gid, callback) Asynchronous fchown(). No arguments other than a possible exception are given to the completion callback.
9 fs.fchownSync(fd, uid, gid) Synchronous fchown().
10 fs.lchown(path, uid, gid, callback) Asynchronous lchown(). No arguments other than a possible exception are given to the completion callback.
11 fs.lchownSync(path, uid, gid) Synchronous lchown().
12 fs.chmod(path, mode, callback) Asynchronous chmod(). No arguments other than a possible exception are given to the completion callback.
13 fs.chmodSync(path, mode) Synchronous chmod().
14 fs.fchmod(fd, mode, callback) Asynchronous fchmod(). No arguments other than a possible exception are given to the completion callback.
15 fs.fchmodSync(fd, mode) Synchronous fchmod().
16 fs.lchmod(path, mode, callback) Asynchronous lchmod(). No arguments other than a possible exception are given to the completion callback. Only available on Mac OS X.
17 fs.lchmodSync(path, mode) Synchronous lchmod().
18 fs.stat(path, callback) Asynchronous stat(). The callback gets two arguments (err, stats) where stats is an fs.Stats object.
19 fs.lstat(path, callback) Asynchronous lstat(). The callback gets two arguments (err, stats) where stats is an fs.Stats object. lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
20 fs.fstat(fd, callback) Asynchronous fstat(). The callback gets two arguments (err, stats) where stats is an fs.Stats object. fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.
21 fs.statSync(path) Synchronous stat(). Returns an instance of fs.Stats.
22 fs.lstatSync(path) Synchronous lstat(). Returns an instance of fs.Stats.
23 fs.fstatSync(fd) Synchronous fstat(). Returns an instance of fs.Stats.
24 fs.link(srcpath, dstpath, callback) Asynchronous link(). No arguments other than a possible exception are given to the completion callback.
25 fs.linkSync(srcpath, dstpath) Synchronous link().
26 fs.symlink(srcpath, dstpath[, type], callback) Asynchronous symlink(). No arguments other than a possible exception are given to the completion callback. The type argument can be set to 'dir', 'file', or 'junction' (default is 'file') and is only available on Windows (ignored on other platforms). Note that Windows junction points require the destination path to be absolute. When using 'junction', the destination argument will automatically be normalized to absolute path.
27 fs.symlinkSync(srcpath, dstpath[, type]) Synchronous symlink().
28 fs.readlink(path, callback) Asynchronous readlink(). The callback gets two arguments (err, linkString).
29 fs.realpath(path[, cache], callback) Asynchronous realpath(). The callback gets two arguments (err, resolvedPath). May use process.cwd to resolve relative paths. cache is an object literal of mapped paths that can be used to force a specific path resolution or avoid additional fs.stat calls for known real paths.
30 fs.realpathSync(path[, cache]) Synchronous realpath(). Returns the resolved path.
31 fs.unlink(path, callback) Asynchronous unlink(). No arguments other than a possible exception are given to the completion callback.
32 fs.unlinkSync(path) Synchronous unlink().
33 fs.rmdir(path, callback) Asynchronous rmdir(). No arguments other than a possible exception are given to the completion callback.
34 fs.rmdirSync(path) Synchronous rmdir().
35 fs.mkdir(path[, mode], callback) Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback. mode defaults to 0777.
36 fs.mkdirSync(path[, mode]) Synchronous mkdir().
37 fs.readdir(path, callback) Asynchronous readdir(3). Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.
38 fs.readdirSync(path) Synchronous readdir(). Returns an array of filenames excluding '.' and '..'.
39 fs.close(fd, callback) Asynchronous close(). No arguments other than a possible exception are given to the completion callback.
40 fs.closeSync(fd) Synchronous close().
41 fs.open(path, flags[, mode], callback) Asynchronous file open.
42 fs.openSync(path, flags[, mode]) Synchronous version of fs.open().
43 fs.utimes(path, atime, mtime, callback) 44 fs.utimesSync(path, atime, mtime) Change file timestamps of the file referenced by the supplied path.
45 fs.futimes(fd, atime, mtime, callback) 46 fs.futimesSync(fd, atime, mtime) Change the file timestamps of a file referenced by the supplied file descriptor.
47 fs.fsync(fd, callback) Asynchronous fsync. No arguments other than a possible exception are given to the completion callback.
48 fs.fsyncSync(fd) Synchronous fsync.
49 fs.write(fd, buffer, offset, length[, position], callback) Write buffer to the file specified by fd.
50 fs.write(fd, data[, position[, encoding]], callback) Write data to the file specified by fd. If data is not a Buffer instance then the value will be coerced to a string.
51 fs.writeSync(fd, buffer, offset, length[, position]) Synchronous versions of fs.write(). Returns the number of bytes written.
52 fs.writeSync(fd, data[, position[, encoding]]) Synchronous versions of fs.write(). Returns the number of bytes written.
53 fs.read(fd, buffer, offset, length, position, callback) Read data from the file specified by fd.
54 fs.readSync(fd, buffer, offset, length, position) Synchronous version of fs.read. Returns the number of bytesRead.
55 fs.readFile(filename[, options], callback) Asynchronously reads the entire contents of a file.
56 fs.readFileSync(filename[, options]) Synchronous version of fs.readFile. Returns the contents of the filename.
57 fs.writeFile(filename, data[, options], callback) Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.
58 fs.writeFileSync(filename, data[, options]) The synchronous version of fs.writeFile.
59 fs.appendFile(filename, data[, options], callback) Asynchronously append data to a file, creating the file if it does not exist. data can be a string or a buffer.
60 fs.appendFileSync(filename, data[, options]) The synchronous version of fs.appendFile.
61 fs.watchFile(filename[, options], listener) Watch for changes on filename. The callback listener will be called each time the file is accessed.
62 fs.unwatchFile(filename[, listener]) Stop watching for changes on filename. If listener is specified, only that particular listener is removed. Otherwise, all listeners are removed and you have effectively stopped watching filename.
63 fs.watch(filename[, options][, listener]) Watch for changes on filename, where filename is either a file or an directory. The returned object is an fs.FSWatcher.
64 fs.exists(path, callback) Test whether or not the given path exists by checking with the file system. Then call the callback argument with either true or false.
65 fs.existsSync(path) Synchronous version of fs.exists.
66 fs.access(path[, mode], callback) Tests a user's permissions for the file specified by path. mode is an optional integer that specifies the accessibility checks to be performed.
67 fs.accessSync(path[, mode]) Synchronous version of fs.access. It throws if any accessibility checks fail, and does nothing otherwise.
68 fs.createReadStream(path[, options]) Returns a new ReadStream object.
69 fs.createWriteStream(path[, options]) Returns a new WriteStream object.
70 fs.symlink(srcpath, dstpath[, type], callback) Asynchronous symlink(). No arguments other than a possible exception are given to the completion callback. The type argument can be set to 'dir', 'file', or 'junction' (default is 'file') and is only available on Windows (ignored on other platforms). Note that Windows junction points require the destination path to be absolute. When using 'junction', the destination argument will automatically be normalized to absolute path.