Table of Contents generated with DocToc
node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装
文件操作的API有异步/同步两种
在异步操作API后加上Sync,并去除callback,即可得同步操作API
eg.
fs.readFile(file[, options], callback);
fs.readFileSync(file[, options]);
fs.open(path, flags[, mode], callback);
fs.openSync(path, flags[, mode]);
fs.FSWatcher- Event:
change - Event:
error - Function:
watcher.close()
- Event:
fs.FSWatcher 是 fs.watch() 返回的class类型
fs.ReadStream- Event:
open- 当文件的流被打开时触发
- Property:
readStream.path- 流的路径
- Event:
fs.ReadStream 是可读的数据流
fs.WriteStream- Event:
open- 当文件的流被打开时触发
- Property:
writeStream.bytesWritten- 已写入的字节数。不包括还在队列中的未写入字节
- Property:
writeStream.path- 流被写入的路径
- Event:
fs.WriteStream 是可写的数据流
fs.Stats- Function:
stats.isFile() - Function:
stats.isDirectory() - Function:
stats.isBlockDevice() - Function:
stats.isCharacterDevice() - Function:
stats.isSymbolicLink() - Function:
stats.isFIFO() - Function:
stats.isSocket()
- Function:
fs.Stats 是 fs.stat(), fs.lstat() 和 fs.fstat() 以及他们对应的同步版本返回的对象
fs.access(path[, mode], callback);
fs.accessSync(path[, mode]);Tests a user's permissions for the file specified by
path
modeis an optional integer that specifies the accessibility checks to be performed检查用户对文件操作的权限。
mode传入代表文件操作的fs常量
mode的取值:
- fs.F_OK
- default.文件可见。常用于确定文件是否存在
- fs.R_OK
- 文件可读
- fs.W_OK
- 文件可写
- fs.X_OK
- 文件可删除(Windows下无效)
检查一个文件是否存在
fs.existe(path, callback);
fs.existsSync(path);- callback(exists); exists为是否存在的布尔值
var fs= require("fs");
fs.exists('/etc/passwd', function (exists) {
console.log(exists ? "存在" : "不存在!");
});fs.appendFile(filename, data, [options], callback);
fs.appendFileSync(filename, data, [options]);Asynchronously append data to a file, creating the file if it does not yet exist. data can be a string or a buffer
将data添加到file文件后面。如果文件不存在就新建文件。data可以是string或者buffer
options的配置(可以是String或者Object):
encoding: String | Null, default = 'uft8'mode: Number, default = 0o666flag: String, default = 'a'
example:
fs.appendFile('message.txt', 'data to append', 'utf8', (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});Note :
Any specified file descriptor has to have been opened for appending.
Specified file descriptors will not be closed automatically.
fs.rename(oldPath, newPath, callback);
fs.renameSync(oldPath, newPath);callback是失败时的回调,接收可能的异常信息作为参数
fs.close(fd, callback)
fs.closeSync(fd)fs.createReadStream(path[, options])Returns a new ReadStream object
options is an object or string with the following defaults:
options可以是一个对象或者字符串,如下是其默认配置
options can include start and end values to read a range of bytes from the file instead of the entire file
options可以包含start和end值,以便读取file中的一段数据而不必全部读取
{
flags: 'r',
encoding: null,
fd: null,
mode: 0o666,
autoClose: true
}If
fdis specified, ReadStream will ignore the path argument and will use the specified file descriptor. This means that no 'open' event will be emitted.如果特定指明了
fd,ReadStream则会忽略path参数并使用指定的file descriptor(fd),那么open行为并不会发生If
autoCloseis false, then the file descriptor won't be closed, even if there's an error.当
autoClose为false的时候,即便发生错误file descriptor也不会被关闭If
autoCloseis set to true (default behavior), on error or end the file descriptor will be closed automatically.当
autoClose为true时,file descriptor会在发生错误或结束时自动关闭If options is a string, then it specifies the encoding.
如果options为字符串,那么应该是指明encoding的字符串
fs.createWriteStream(path[, options])Returns a new WriteStream object
options is an object or string with the following defaults:
{
flags: 'w',
defaultEncoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
}options may also include a start option to allow writing data at some position past the beginning of the file.
options可以包含一个起始值,用于指明从文件的哪个位置开始写入数据
Modifying a file rather than replacing it may require a flags mode of r+ rather than the default mode w.
如果是继续写入数据而不是替换原有数据的话,需要将
flags设置为r+而不是w
autoClose、fd特性和fs.createReadStream()中相同If options is a string, then it specifies the encoding.
如果options为字符串,那么应该是指明encoding的字符串
fs.open(path, flags[, mode], callback)
fs.openSync(path, flags[, mode])flags can be the following:
r读取文件。文件不存在的时候会报错r+读写文件。文件不存在的时候会报错rs以同步模式读取文件。请使用fs.openSync()rs+以同步模式读写文件w写文件。文件不存在的时候则会新建文件wx与w功能类似,但是path已存在的时候则无效w+读写文件。文件不存在的时候则会新建文件wx+与w+功能类似,但是path已存在的时候则无效a在文件末尾续写。当文件不存在的时候则新建ax与a功能类似,但是path已存在的时候则无效a+读取/续写文件。当文件不存在的时候则新建ax+与a+功能类似,但是path已存在的时候则无效
fs.read(fd, buffer, offset, length, position, callback)Read data from the file specified by fd.
buffer缓冲区,数据将写入到这里offset向缓冲区写入的数据量lengthinteger,写入数据的字节数positioninteger,指定了从哪里开始读取文件。当值为null的时候callback(err, bytesRead, buffer)分别为错误、读取的字节和缓冲区
fs.write(fd, buffer, offset, length[, position], callback)
fs.writeSync(fd, buffer, offset, length[, position])
fs.write(fd, data[, position[, encoding]], callback)
fs.writeSync(fd, data[, position[, encoding]])fs.readdir(path, callback)
fs.readdirSync(path)读取path路径所在目录下的所有文件
callback接收两个参数:error和files
其中files 是一个存储目录中所包含的文件名称的数组,数组中不包括 '.' 和 '..'
fs.readFile(file[, options], callback)
fs.readFileSync(file[, options])fileoptionsObject或StringencodingString或null, default = nullflagdefault =r
callback(error, data)
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
fs.readFile('/etc/passwd', 'utf8', callback);fs.writeFile(file, data[, options], callback)
fs.writeFileSync(file, data[, options])filedataoptionsencodingString | Null, default = 'utf8'modeNumber, default = 0o66flagString, default =w
callback(error)// 回调只有error
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);如果文件已经存在则会被替换掉
it is unsafe to use fs.writeFile multiple times on the same file without waiting for the callback
fs.rename(oldPath, newPath, callback)
fs.renameSync(oldPath, newPath)// 删除目录
fs.rmdir(path, callback)
fs.rmdirSync(path)
// 创建目录
fs.mkdir(path, [mode], callback)
fs.mkdirSync(path, [mode])mode权限,默认为0777,表示所有用户都可进行读/写/执行的操作callback(error)
fs.unlink(path, callback);
fs.unlinkSync(path);删除文件
var fs = require('fs');
fs.unlink(文件, function(err) {
if (err) throw err;
console.log('successfully deleted');
});