File System

Class: fs.Stats

fs.Stats class is an object returned from fs.stat(),fs.fstat() and their synchronous counterparts.

stats.isDirectory()

  • Returns: {boolean}

Returns true if stated file is a directory.

stats.isFile()

  • Returns: {boolean}

Returns true if stated file is a file.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. fs.stat('test.txt', function(err, stat) {
  4. if (err) {
  5. throw err;
  6. }
  7. assert.equal(stat.isFile(), true);
  8. assert.equal(stat.isDirectory(), false);
  9. });

fs.close(fd, callback)

  • fd {integer} File descriptor.
  • callback {Function}
    • err {Error|null}

Closes the file of fd asynchronously.

Example

  1. var fs = require('fs');
  2. fs.open('test.txt', 'r', function(err, fd) {
  3. if (err) {
  4. throw err;
  5. }
  6. // do something
  7. fs.close(fd, function(err) {
  8. if (err) {
  9. throw err;
  10. }
  11. });
  12. });

fs.closeSync(fd)

  • fd {integer} File descriptor.

Closes the file of fd synchronously.

Example

  1. var fs = require('fs');
  2. var fd = fs.openSync('test.txt', 'r');
  3. // do something
  4. fs.closeSync(fd);

Class: fs.ReadStream

A successful call to fs.createReadStream() will return a new fs.ReadStream object.

fs.ReadStream inherits from stream.Readable.

Event: ‘open’

  • fd {integer} File descriptor used by the fs.ReadStream.

Emitted when the fs.ReadStream‘s file descriptor has been opened.

Event: ‘ready’

Emitted when the fs.ReadStream is ready to be used. Emitted immediately after open.

Event: ‘data’

  • chunk {Buffer|string}

Inherited from stream.Readable. Emitted when the stream passes the ownership of the data to a consumer. Only streams in flowing mode emit this event.

A stream can be switched to flowing mode by calling the readable.resume() function or by adding a ‘data’ event handler.

Event: ‘close’

Emitted when the fs.ReadStream‘s file descriptor has been closed.

ReadStream.bytesRead

  • {integer}

Number of bytes that have been read so far.

ReadStream.path

  • {string}

The path to the file of the fs.ReadStream.

fs.createReadStream(path[, options])

  • path {string} File path to open for reading.
  • options {Object}
    • flags {string} Flags to open file with. Default: 'r'
    • encoding {string} Default: null
    • fd {integer} File descriptor to be used. Default: null
    • mode {integer} Permission mode. Default: 0666
    • autoClose {boolean} Should the file be closed automatically. Default: true
    • bufferSize {integer} Size of buffer in bytes. Default: 4096
  • Returns: fs.ReadStream

If fd is specified, path will be ignored and the specified file descriptor will be used instead.

If autoClose is false, the file will not be closed automatically, even if there is an error, it will be the application’s responsibility. If it is true (as by default), the file will be closed automatically when end of file is reached or the stream ends.

Example

  1. var fs = require('fs');
  2. var rStream = fs.createReadStream('example.txt');
  3. rStream.on('data', function(data) {
  4. console.log(data.toString());
  5. });

fs.ReadStream inherits from stream.Readable, so it is possible to pipe it into any stream.Writable.

Example

  1. var fs = require('fs');
  2. var readableFileStream = fs.createReadStream('in.txt');
  3. var writableFileStream = fs.createWriteStream('out.txt');
  4. // The content of in.txt will be copied to out.txt
  5. readableFileStream.pipe(writableFileStream);

Class: fs.WriteStream

A successful call to fs.createWriteStream() will return a new fs.WriteStream object.

fs.WriteStream inherits from stream.Writable.

Event: ‘open’

  • fd {integer} File descriptor used by the fs.WriteStream.

Emitted when the fs.WriteStream‘s file descriptor has been opened.

Event: ‘ready’

Emitted when the fs.WriteStream is ready to be used. Emitted immediately after open.

Event: ‘close’

Emitted when the fs.WriteStream‘s file descriptor has been closed.

WriteStream.bytesWritten

The number of bytes written so far. Does not include data that is still queued for writing.

WriteStream.path

The path to the file of the fs.WriteStream.

fs.createWriteStream

  • path {string} File path to be opened for writing.
  • options {Object}
    • flags {string} Flags to open the file with. Default: 'w'
    • fd {integer} File descriptor to be used. Default: null
    • mode {integer} Permission mode. Default: 0666
    • autoClose {boolean} Should the file be closed automatically. Default: true
  • Returns fs.WriteStream

Works similarly to fs.createReadStream(), but returns an fs.WriteStream.

If fd is specified, path will be ignored and the specified file descriptor will be used instead.

If autoClose is false, the file will not be closed automatically, even if there is an error, it will be the application’s responsibility. If it is true (as by default), the file will be closed automatically when end of file is reached or the stream ends.

Example

  1. var fs = require('fs');
  2. var wStream = fs.createWriteStream('example.txt');
  3. wStream.on('ready', function() {
  4. wStream.write('test data');
  5. // 'test data' will be written into example.txt
  6. });

fs.exists(path, callback)

  • path {string} File path to be checked.
  • callback {Function}
    • exists {boolean}

Checks the file specified by path exists asynchronously.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. fs.exists('test.txt', function(exists) {
  4. assert.equal(exists, true);
  5. });

fs.existsSync(path)

  • path {string} File path to be checked.
  • Returns: {boolean} True if the file exists, otherwise false.

Checks the file specified by path exists synchronously.

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. var result = fs.existsSync('test.txt');
  4. assert.equal(result, true);

fs.fstat(fd, callback)

  • fd {integer} File descriptor to be stated.
  • callback {Function}
    • err {Error|null}
    • stat {Object} An instance of fs.Stats.

Get information about a file what specified by fd into stat asynchronously.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. fs.open('test.txt', 'r', function(err, fd) {
  4. if (err) {
  5. throw err;
  6. }
  7. fs.fstat(fd, function(err, stat) {
  8. if (err) {
  9. throw err;
  10. }
  11. assert.equal(stat.isFile(), true);
  12. assert.equal(stat.isDirectory(), false);
  13. });
  14. });

fs.fstatSync(fd)

  • fd {integer} - File descriptor to be stated.
  • Returns: {Object} An instance of fs.Stats.

Get information about a file what specified by fd synchronously.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. fs.open('test.txt', 'r', function(err, fd) {
  4. if (err) {
  5. throw err;
  6. }
  7. var stat = fs.fstatSync(fd);
  8. assert.equal(stat.isFile(), true);
  9. assert.equal(stat.isDirectory(), false);
  10. });

fs.mkdir(path[, mode], callback)

  • path {string} Path of the directory to be created.
  • mode {string|number} Permission mode. Default: 0777
  • callback {Function}
    • err {Error|null}

Creates the directory specified by path asynchronously.

Example

  1. var fs = require('fs');
  2. fs.mkdir('testdir', function(err) {
  3. if (err) {
  4. throw err;
  5. }
  6. });

fs.mkdirSync(path[, mode])

  • path {string} Path of the directory to be created.
  • mode {string|number} Permission mode. Default: 0777

Creates the directory specified by path synchronously.

Example

  1. var fs = require('fs');
  2. fs.mkdirSync('testdir');

fs.open(path, flags[, mode], callback)

  • path {string} File path to be opened.
  • flags {string} Open flags.
  • mode {string|number} Permission mode. Default: 0666
  • callback {Function}
    • err {Error|null}
    • fd {number}

Opens file asynchronously.

flags can be:

  • r Opens file for reading. Throws an exception if the file does not exist.
  • rs or sr Opens file for reading in synchronous mode. Throws an exception if the file does not exist.
  • r+ Opens file for reading and writing. Throws an exception if the file does not exist.
  • rs+ or sr+ Opens file for reading and writing in synchronous mode. Throws an exception if the file does not exist.
  • w Opens file for writing. The file is overwritten if it exists.
  • wx or xw Opens file for writing. Throws an exception if it exists.
  • w+ Opens file for reading and writing. The file is overwritten if it exists.
  • wx+ or xw+ Opens file for reading and writing. Throws an exception if it exists.
  • a Opens file for appending. The file is created if it does not exist.
  • ax or xa Opens file for appending. Throws an exception if it exists.
  • a+ Opens file for reading and appending. The file is created if it does not exist.
  • ax+ or xa+ Opens file for reading and appending. Throws an exception if it exists.

Example

  1. var fs = require('fs');
  2. fs.open('test.txt', 'r', 755, function(err, fd) {
  3. if (err) {
  4. throw err;
  5. }
  6. // do something
  7. });

fs.openSync(path, flags[, mode])

  • path {string} File path to be opened.
  • flags {string} Open flags.
  • mode {string|number} Permission mode. Default: 0666
  • Returns: {number} File descriptor.

Opens file synchronously.

For available options of the flags see fs.open().

Example

  1. var fs = require('fs');
  2. var fd = fs.openSync('test.txt', 'r', 755);
  3. // do something

fs.read(fd, buffer, offset, length, position, callback)

  • fd {integer} File descriptor.
  • buffer {Buffer} Buffer that the data will be written to.
  • offset {number} Offset of the buffer where to start writing.
  • length {number} Number of bytes to read.
  • position {number} Specifying where to start read data from the file, if null or undefined, read from current position.
  • callback {Function}
    • err {Error|null}
    • bytesRead {number}
    • buffer {Buffer}

Reads data from the file specified by fd asynchronously.

Example

  1. var fs = require('fs');
  2. fs.open('test.txt', 'r', 755, function(err, fd) {
  3. if (err) {
  4. throw err;
  5. }
  6. var buffer = new Buffer(64);
  7. fs.read(fd, buffer, 0, buffer.length, 0, function(err, bytesRead, buffer) {
  8. if (err) {
  9. throw err;
  10. }
  11. });
  12. });

fs.readSync(fd, buffer, offset, length, position)

  • fd {integer} File descriptor.
  • buffer {Buffer} Buffer that the data will be written to.
  • offset {number} Offset of the buffer where to start writing.
  • length {number} Number of bytes to read.
  • position {number} Specifying where to start read data from the file, if null or undefined, read from current position.
  • Returns: {number} Number of read bytes.

Reads data from the file specified by fd synchronously.

Example

  1. var fs = require('fs');
  2. var buffer = new Buffer(16);
  3. var fd = fs.openSync('test.txt', 'r');
  4. var bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);

fs.readdir(path, callback)

  • path {string} Directory path to be checked.
  • callback {Function}
    • err {Error|null}
    • files {Object}

Reads the contents of the directory specified by path asynchronously, . and .. are excluded from files.

Example

  1. var fs = require('fs');
  2. fs.readdir('testdir', function(err, items) {
  3. if (err) {
  4. throw err;
  5. }
  6. // prints: file1,file2,... from 'testdir'
  7. console.log(items);
  8. });

fs.readdirSync(path)

  • path {string} Directory path to be checked.
  • Returns: {Object} Array of filenames.

Reads the contents of the directory specified by path synchronously, . and .. are excluded from filenames.

Example

  1. var fs = require('fs');
  2. var items = fs.readdirSync('testdir');
  3. // prints: file1,file2,... from 'testdir'
  4. console.log(items);

fs.readFile(path, callback)

  • path {string} File path to be opened.
  • callback {Function}
    • err {Error|null}
    • data {Buffer}

Reads entire file asynchronously into data.

Example

  1. var fs = require('fs');
  2. fs.readFile('test.txt', function(err, data) {
  3. if (err) {
  4. throw err;
  5. }
  6. // prints: the content of 'test.txt'
  7. console.log(data);
  8. });

fs.readFileSync(path)

  • path {string} File path to be opened.
  • Returns: {Object} Contents of the file.

Reads entire file synchronously.

Example

  1. var fs = require('fs');
  2. var data = fs.readFileSync('test.txt');

fs.rename(oldPath, newPath, callback)

  • oldPath {string} Old file path.
  • newPath {string} New file path.
  • callback {Function}
    • err {Error|null}

Renames oldPath to newPath asynchronously.

Example

  1. var fs = require('fs');
  2. fs.rename('test.txt', 'test.txt.async', function(err) {
  3. if (err) {
  4. throw err;
  5. }
  6. });

fs.renameSync(oldPath, newPath)

  • oldPath {string} Old file path.
  • newPath {string} New file path.

Renames oldPath to newPath synchronously.

Example

  1. var fs = require('fs');
  2. fs.renameSync('test.txt', 'test.txt.sync');

fs.rmdir(path, callback)

  • path {string} Directory path to be removed.
  • callback {Function}
    • err {Error|null}

Removes the directory specified by path asynchronously.

Example

  1. var fs = require('fs');
  2. fs.rmdir('testdir', function() {
  3. // do something
  4. });

fs.rmdirSync(path)

  • path {string} Directory path to be removed.

Removes the directory specified by path synchronously.

  1. var fs = require('fs');
  2. fs.rmdirSync('testdir');

fs.stat(path, callback)

  • path {string} File path to be stated.
  • callback {Function}
    • err {Error|null}
    • stat {Object}

Get information about a file into stat asynchronously.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. fs.stat('test.txt', function(err, stat) {
  4. if (err) {
  5. throw err;
  6. }
  7. assert.equal(stat.isFile(), true);
  8. assert.equal(stat.isDirectory(), false);
  9. });

fs.statSync(path)

  • path {string} File path to be stated.
  • Returns: {Object} An instance of fs.Stats.

Get information about a file synchronously.

Example

  1. var assert = require('assert');
  2. var fs = require('fs');
  3. var stat = fs.statSync('test.txt');
  4. assert.equal(stat.isFile(), true);
  5. assert.equal(stat.isDirectory(), false);

fs.unlink(path, callback)

  • path {string} File path to be removed.
  • callback {Function}
    • err {Error|null}

Removes the file specified by path asynchronously.

Example

  1. var fs = require('fs');
  2. fs.unlink('test.txt', function(err) {
  3. if (err) {
  4. throw err;
  5. }
  6. });

fs.unlinkSync(path)

  • path {string} File path to be removed.

Removes the file specified by path synchronously.

Example

  1. var fs = require('fs');
  2. fs.unlinkSync('test.txt');

fs.write(fd, buffer, offset, length[, position], callback)

  • fd {integer} File descriptor.
  • buffer {Buffer} Buffer that the data will be written from.
  • offset {number} Offset of the buffer where from start reading.
  • length {number} Number of bytes to write.
  • position {number} Specifying where to start write data to the file, if null or undefined, write at the current position.
  • callback {Function}
    • err {Error|null}
    • bytesWrite {integer}
    • buffer {Object}

Writes buffer to the file specified by fd asynchronously.

Example

  1. var fs = require('fs');
  2. var file = 'test.txt'
  3. var data = new Buffer('IoT.js');
  4. fs.open(file, 'w', function(err, fd) {
  5. if (err) {
  6. throw err;
  7. }
  8. fs.write(fd, data, 0, data.length, function(err, bytesWrite, buffer) {
  9. if (err) {
  10. throw err;
  11. }
  12. // prints: 6
  13. console.log(bytesWrite);
  14. // prints: IoT.js
  15. console.log(buffer);
  16. });
  17. });

fs.writeSync(fd, buffer, offset, length[, position])

  • fd {integer} File descriptor.
  • buffer {Buffer} Buffer that the data will be written from.
  • offset {number} Offset of the buffer where from start reading.
  • length {number} Number of bytes to write.
  • position {number} Specifying where to start write data to the file, if null or undefined, write at the current position.
  • Returns: {number} Number of bytes written.

Writes buffer to the file specified by fd synchronously.

  1. var fs = require('fs');
  2. var file = 'test.txt'
  3. var data = new Buffer('IoT.js');
  4. var fd = fs.openSync(file, 'w');
  5. var bytes = fs.writeSync(fd, data, 0, data.length);
  6. //prints: 6
  7. console.log(bytes);

fs.writeFile(path, data, callback)

  • path {string} File path that the data will be written.
  • data {string|Buffer} String or buffer that contains data.
  • callback {Function}
    • err {Error|null}

Writes entire data to the file specified by path asynchronously.

Example

  1. var fs = require('fs');
  2. fs.writeFile('test.txt', 'IoT.js', function(err) {
  3. if (err) {
  4. throw err;
  5. }
  6. });

fs.writeFileSync(path, data)

  • path {string} File path that the data will be written.
  • data {string|Buffer} String or buffer that contains data.

Writes entire data to the file specified by path synchronously.

Example

  1. var fs = require('fs');
  2. fs.writeFileSync('test.txt', 'IoT.js');