fs.Stats class is an object returned from fs.stat()
,fs.fstat()
and their synchronous counterparts.
Returns true if stated file is a directory.
Returns true if stated file is a file.
Example
var assert = require('assert');
var fs = require('fs');
fs.stat('test.txt', function(err, stat) {
if (err) {
throw err;
}
assert.equal(stat.isFile(), true);
assert.equal(stat.isDirectory(), false);
});
fd
{integer} File descriptor.callback
{Function}
err
{Error|null}Closes the file of fd
asynchronously.
Example
var fs = require('fs');
fs.open('test.txt', 'r', function(err, fd) {
if (err) {
throw err;
}
// do something
fs.close(fd, function(err) {
if (err) {
throw err;
}
});
});
fd
{integer} File descriptor.Closes the file of fd
synchronously.
Example
var fs = require('fs');
var fd = fs.openSync('test.txt', 'r');
// do something
fs.closeSync(fd);
A successful call to fs.createReadStream()
will return a new fs.ReadStream
object.
fs.ReadStream
inherits from stream.Readable
.
fd
{integer} File descriptor used by the fs.ReadStream
.Emitted when the fs.ReadStream
's file descriptor has been opened.
Emitted when the fs.ReadStream
is ready to be used. Emitted immediately
after open
.
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.
Emitted when the fs.ReadStream
's file descriptor has been closed.
Number of bytes that have been read so far.
The path to the file of the fs.ReadStream
.
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
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
var fs = require('fs');
var rStream = fs.createReadStream('example.txt');
rStream.on('data', function(data) {
console.log(data.toString());
});
fs.ReadStream
inherits from stream.Readable
, so it is possible to pipe
it into any stream.Writable
.
Example
var fs = require('fs');
var readableFileStream = fs.createReadStream('in.txt');
var writableFileStream = fs.createWriteStream('out.txt');
// The content of in.txt will be copied to out.txt
readableFileStream.pipe(writableFileStream);
A successful call to fs.createWriteStream()
will return a new fs.WriteStream
object.
fs.WriteStream
inherits from stream.Writable
.
fd
{integer} File descriptor used by the fs.WriteStream
.Emitted when the fs.WriteStream
's file descriptor has been opened.
Emitted when the fs.WriteStream
is ready to be used. Emitted immediately
after open
.
Emitted when the fs.WriteStream
's file descriptor has been closed.
The number of bytes written so far. Does not include data that is still queued for writing.
The path to the file of the fs.WriteStream
.
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
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
var fs = require('fs');
var wStream = fs.createWriteStream('example.txt');
wStream.on('ready', function() {
wStream.write('test data');
// 'test data' will be written into example.txt
});
path
{string} File path to be checked.callback
{Function}
exists
{boolean}Checks the file specified by path
exists asynchronously.
Example
var assert = require('assert');
var fs = require('fs');
fs.exists('test.txt', function(exists) {
assert.equal(exists, true);
});
path
{string} File path to be checked.Checks the file specified by path
exists synchronously.
var assert = require('assert');
var fs = require('fs');
var result = fs.existsSync('test.txt');
assert.equal(result, true);
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
var assert = require('assert');
var fs = require('fs');
fs.open('test.txt', 'r', function(err, fd) {
if (err) {
throw err;
}
fs.fstat(fd, function(err, stat) {
if (err) {
throw err;
}
assert.equal(stat.isFile(), true);
assert.equal(stat.isDirectory(), false);
});
});
fd
{integer} - File descriptor to be stated.fs.Stats
.Get information about a file what specified by fd
synchronously.
Example
var assert = require('assert');
var fs = require('fs');
fs.open('test.txt', 'r', function(err, fd) {
if (err) {
throw err;
}
var stat = fs.fstatSync(fd);
assert.equal(stat.isFile(), true);
assert.equal(stat.isDirectory(), false);
});
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
var fs = require('fs');
fs.mkdir('testdir', function(err) {
if (err) {
throw err;
}
});
path
{string} Path of the directory to be created.mode
{string|number} Permission mode. Default: 0777
Creates the directory specified by path
synchronously.
Example
var fs = require('fs');
fs.mkdirSync('testdir');
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
var fs = require('fs');
fs.open('test.txt', 'r', 755, function(err, fd) {
if (err) {
throw err;
}
// do something
});
path
{string} File path to be opened.flags
{string} Open flags.mode
{string|number} Permission mode. Default: 0666
Opens file synchronously.
For available options of the flags
see fs.open().
Example
var fs = require('fs');
var fd = fs.openSync('test.txt', 'r', 755);
// do something
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
var fs = require('fs');
fs.open('test.txt', 'r', 755, function(err, fd) {
if (err) {
throw err;
}
var buffer = new Buffer(64);
fs.read(fd, buffer, 0, buffer.length, 0, function(err, bytesRead, buffer) {
if (err) {
throw err;
}
});
});
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.Reads data from the file specified by fd
synchronously.
Example
var fs = require('fs');
var buffer = new Buffer(16);
var fd = fs.openSync('test.txt', 'r');
var bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);
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
var fs = require('fs');
fs.readdir('testdir', function(err, items) {
if (err) {
throw err;
}
// prints: file1,file2,... from 'testdir'
console.log(items);
});
path
{string} Directory path to be checked.Reads the contents of the directory specified by path
synchronously, .
and ..
are excluded from filenames.
Example
var fs = require('fs');
var items = fs.readdirSync('testdir');
// prints: file1,file2,... from 'testdir'
console.log(items);
path
{string} File path to be opened.callback
{Function}
err
{Error|null}data
{Buffer}Reads entire file asynchronously into data
.
Example
var fs = require('fs');
fs.readFile('test.txt', function(err, data) {
if (err) {
throw err;
}
// prints: the content of 'test.txt'
console.log(data);
});
path
{string} File path to be opened.Reads entire file synchronously.
Example
var fs = require('fs');
var data = fs.readFileSync('test.txt');
oldPath
{string} Old file path.newPath
{string} New file path.callback
{Function}
err
{Error|null}Renames oldPath
to newPath
asynchronously.
Example
var fs = require('fs');
fs.rename('test.txt', 'test.txt.async', function(err) {
if (err) {
throw err;
}
});
oldPath
{string} Old file path.newPath
{string} New file path.Renames oldPath
to newPath
synchronously.
Example
var fs = require('fs');
fs.renameSync('test.txt', 'test.txt.sync');
path
{string} Directory path to be removed.callback
{Function}
err
{Error|null}Removes the directory specified by path
asynchronously.
Example
var fs = require('fs');
fs.rmdir('testdir', function() {
// do something
});
path
{string} Directory path to be removed.Removes the directory specified by path
synchronously.
var fs = require('fs');
fs.rmdirSync('testdir');
path
{string} File path to be stated.callback
{Function}
err
{Error|null}stat
{Object}Get information about a file into stat
asynchronously.
Example
var assert = require('assert');
var fs = require('fs');
fs.stat('test.txt', function(err, stat) {
if (err) {
throw err;
}
assert.equal(stat.isFile(), true);
assert.equal(stat.isDirectory(), false);
});
path
{string} File path to be stated.fs.Stats
.Get information about a file synchronously.
Example
var assert = require('assert');
var fs = require('fs');
var stat = fs.statSync('test.txt');
assert.equal(stat.isFile(), true);
assert.equal(stat.isDirectory(), false);
path
{string} File path to be removed.callback
{Function}
err
{Error|null}Removes the file specified by path
asynchronously.
Example
var fs = require('fs');
fs.unlink('test.txt', function(err) {
if (err) {
throw err;
}
});
path
{string} File path to be removed.Removes the file specified by path
synchronously.
Example
var fs = require('fs');
fs.unlinkSync('test.txt');
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
var fs = require('fs');
var file = 'test.txt'
var data = new Buffer('IoT.js');
fs.open(file, 'w', function(err, fd) {
if (err) {
throw err;
}
fs.write(fd, data, 0, data.length, function(err, bytesWrite, buffer) {
if (err) {
throw err;
}
// prints: 6
console.log(bytesWrite);
// prints: IoT.js
console.log(buffer);
});
});
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.Writes buffer to the file specified by fd
synchronously.
var fs = require('fs');
var file = 'test.txt'
var data = new Buffer('IoT.js');
var fd = fs.openSync(file, 'w');
var bytes = fs.writeSync(fd, data, 0, data.length);
//prints: 6
console.log(bytes);
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
var fs = require('fs');
fs.writeFile('test.txt', 'IoT.js', function(err) {
if (err) {
throw err;
}
});
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
var fs = require('fs');
fs.writeFileSync('test.txt', 'IoT.js');
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。