1. 模块引用
const domain = require('domain');
const fs = require('fs');
const d = domain.create();
2. domain 捕获异常
d.on('error', (err) => {
// an error occurred somewhere.
// if we throw it now, it will crash the program
console.error(err);
});
3. 创建文件夹
fs.mkdir(path[, mode], callback)
参数:path <string> | <Buffer> | <URL>
mode <integer> Default: 0o777
callback <Function>
err <Error>
示例:fs.mkdir('data', d.bind((err, data) => {
if (err) throw err;
console.log('successfully created /data');
}));
如果创建的文件夹存在,则会捕获以下异常结果
{ Error: EEXIST: file already exists, mkdir 'E:\Node.js\Demo\4.fsModule\data'
errno: -4075,
code: 'EEXIST',
syscall: 'mkdir',
path: 'E:\\Node.js\\Demo\\4.fsModule\\data',
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
domainThrown: true }
4. 写文件
如果文件不存在,自动创建文件并写入
fs.writeFile(file, data[, options], callback)
参数:file <string> | <Buffer> | <integer> filename or file descriptor
data <string> | <Buffer> | <Uint8Array>
options <Object> | <string>
encoding <string> | <null> Default: 'utf8'
mode <integer> Default: 0o666
flag <string> Default: 'w'
callback <Function>
err <Error>
示例:
fs.writeFile('data/message.txt', 'Hello ', (err) => {
if (err) throw err;
console.log('successfully written /data/message.txt');
});
5. 获取文件信息
fs.stat(path, callback)
参数:path <string> | <Buffer> | <URL>
callback <Function>
err <Error>
stats <fs.Stats>
示例:
fs.stat('data/message.txt', (err, msg) => {
if (err) {
console.log(err);
} else {
console.log(msg);
console.log(`file: ${msg.isFile()}`);
console.log(`directory: ${msg.isDirectory()}`);
}
});
文件信息:
Stats {
dev: 1379390449,
mode: 33206,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: undefined,
ino: 3377699720534026,
size: 0,
blocks: undefined,
atimeMs: 1515376974470.3665,
mtimeMs: 1515376974470.3665,
ctimeMs: 1515376974470.3665,
birthtimeMs: 1515376974470.3665,
atime: 2018-01-08T02:02:54.470Z,
mtime: 2018-01-08T02:02:54.470Z,
ctime: 2018-01-08T02:02:54.470Z,
birthtime: 2018-01-08T02:02:54.470Z }
6. 文件内容追加
fs.appendFile(file, data[, options], callback)
参数:file <string> | <Buffer> | <number> filename or file descriptor
data <string> | <Buffer>
options <Object> | <string>
encoding <string> | <null> Default: 'utf8'
mode <integer> Default: 0o666
flag <string> Default: 'a'
callback <Function>
err <Error>
示例:fs.appendFile('data/message.txt', 'World!\n', (err) => {
if (err) {
console.log(err);
} else {
console.log('successfully appented /data/message.txt');
}
});
7. 读取文件
fs.readFile(path[, options], callback)
参数:path <string> | <Buffer> | <URL> | <integer> filename or file descriptor
options <Object> | <string>
encoding <string> | <null> Default: null
flag <string> Default: 'r'
callback <Function>
err <Error>
data <string> | <Buffer>
示例:fs.readFile('data/message.txt', 'utf8', (err, data) => {
if (err) {
console.log(err);
} else {
console.log('file content: ' + data);
}
});
8. 读取目录文件
fs.readdir(path[, options], callback)
参数:path <string> | <Buffer> | <URL>
options <string> | <Object>
encoding <string> Default: 'utf8'
callback <Function>
err <Error>
files <string[]> | <Buffer[]>
示例:fs.readdir('data', d.bind((err, files, data) => {
if (err) throw err;
console.log(files);
}));
返回结果:
[ 'message.txt', 'message.xvf' ]
9. 重命名文件
fs.rename(oldPath, newPath, callback)
参数:oldPath <string> | <Buffer> | <URL>
newPath <string> | <Buffer> | <URL>
callback <Function>
err <Error>
示例:fs.rename('data/message.txt', 'data/message.xvf', d.bind((err, data) => {
if (err) throw err;
console.log('successfully renamed data/message.txt -> data/message.xvf');
}));
10. 删除文件
fs.unlink(path, callback)
参数:path <string> | <Buffer> | <URL>
callback <Function>
err <Error>
示例:fs.unlink('data/message.xvf', d.bind((err) => {
if (err) throw err;
console.log('successfully deleted data/message.xvf');
}));
11. 删除目录
fs.rmdir(path, callback)
参数:path <string> | <Buffer> | <URL>
callback <Function>
err <Error>
示例:fs.rmdir('data', d.bind((err) => {
if (err) throw err;
console.log('successfully removed data');
}));