以下是fs模块常用方法,更多具体内容请参考官网:http://nodejs.cn/api/fs.html
// fs是node的核心模块,无需安装
const fs = require('fs');
fs.access(path[, mode], callback)
/**
* 测试用户对 path 指定的文件或目录的权限。
* @param {String|Buffer} path
* @param {Integer} mode 默认值: fs.constants.F_OK
* @param {Function} callback
*/
const file = 'package.json';
// 检查当前目录中是否存在该文件。
fs.access(file, fs.constants.F_OK, (err) => {
console.log(`${file} ${err ? '不存在' : '存在'}`);
});
// 检查文件是否可读。
fs.access(file, fs.constants.R_OK, (err) => {
console.log(`${file} ${err ? '不可读' : '可读'}`);
});
// 检查文件是否可写。
fs.access(file, fs.constants.W_OK, (err) => {
console.log(`${file} ${err ? '不可写' : '可写'}`);
});
// 检查当前目录中是否存在该文件,以及该文件是否可写。
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
console.error(
`${file} ${err.code === 'ENOENT' ? '不存在' : '只可读'}`);
} else {
console.log(`${file} 存在,且它是可写的`);
}
});
不建议在调用 fs.open()、 fs.readFile() 或 fs.writeFile() 之前使用 fs.access() 检查文件的可访问性。 这样做会引入竞态条件,因为其他进程可能会在两个调用之间更改文件的状态。 相反,应该直接打开、读取或写入文件,如果文件无法访问则处理引发的错误。
fs.open(path, flags[, mode], callback)
/**
* 异步地打开文件。
* @param {String|Buffer} path
* @param {String|Number} flags 参阅支持的文件系统标志。
* @param {integer} mode 0o666(可读写)。
* @param {Function} callback
* err
* fd
*/
fs.readFile(path[, options], callback)
/**
* 异步地读取文件的全部内容。
* @param {String|Buffer} path 文件名或文件描述符。
* @param {Object|String} options
* encoding 默认值: null
* flag 参阅支持的文件系统标志。默认值: 'r'
* @param {Function} callback
* err
* data 文件的内容
*/
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
fs.writeFile(file, data[, options], callback)
/**
* 异步地将数据写入到一个文件,如果文件已存在则覆盖该文件。
* @param {String|Buffer|URL|integer} file 文件名或文件描述符。
* @param {String|Buffer|TypedArray|DataView} data
* @param {Object|String} options
* encoding 默认值: null
* mode 默认值: 0o666
* flag 参阅支持的文件系统标志。默认值: 'r'
* @param {Function} callback
* err
*/
const data = new Uint8Array(Buffer.from('Node.js中文网'));
fs.writeFile('文件.txt', data, (err) => {
if (err) throw err;
console.log('文件已被保存');
});
fs.appendFile(path, data[, options], callback)
/**
* 异步地将数据追加到文件,如果文件尚不存在则创建该文件。 data 可以是字符串或 Buffer
* @param {String|Buffer} path 文件名或文件描述符
* @param {String|Buffer} data
* @param {Object|String} options
* encoding 默认值: 'utf8'
* mode 默认值: 0o666
* flag 参阅支持的文件系统标志。默认值: 'a'
* @param {Function} callback
*/
fs.appendFile('message.txt', '追加的数据', (err) => {
if (err) throw err;
console.log('数据已追加到文件');
});
// 如果 options 是字符串,则它指定字符编码:
fs.appendFile('message.txt', '追加的数据', 'utf8', callback);
// path 可以指定为已打开用于追加(使用 fs.open() 或 fs.openSync())的数字型文件描述符。 文件描述符不会自动关闭。
fs.open('message.txt', 'a', (err, fd) => {
if (err) throw err;
fs.appendFile(fd, '追加的数据', 'utf8', (err) => {
fs.close(fd, (err) => {
if (err) throw err;
});
if (err) throw err;
});
});
fs.mkdir(path[, options], callback)
/**
* 异步地创建目录。 除了可能的异常,完成回调没有其他参数。
* @param {String|Buffer} path
* @param {Object|integer} options
* recursive 是否应创建父文件夹 默认值: false
* mode 默认值: 0o777
* @param {Function} callback
*/
// 创建 /tmp/a/apple 目录,无论是否存在 /tmp 和 /tmp/a 目录。
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
if (err) throw err;
});
.
系列文章
【node使用】发布一个自己的npm包
【node使用】搭建一个web服务器
【node使用】package.json详解以及package-lock.json的作用
【node使用】path模块
【node使用】glob匹配模式
【node使用】fs模块
【node使用】实现console输出不同颜色