NodeJs fs 模块基本使用方法
fs(文件系统)
通过 require(‘fs’) 使用该模块。 所有的方法都有异步和同步的形式。
异步版本:1
2
3
4
5
6const fs = require('fs');
fs.unlink('/tmp/hello', (err) => {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
同步版本:1
2
3
4const fs = require('fs');
fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');
基本方法
readFile1
2
3
4
5
6
7fs.readFile(file[, options], callback)
file <String> | <Buffer> | <Integer> 文件名或文件描述符
options <Object> | <String>
encoding <String> | <Null> 默认 = null
flag <String> 默认 = 'r'
callback <Function>
1 | fs.readFile('/etc/passwd', (err, data) => { |
writeFile1
2
3
4
5
6
7
8fs.writeFile(file, data\[, options], callback)
file <String> | <Buffer> | <Integer> 文件名或文件描述符
data <String> | <Buffer>
options <Object> | <String>
encoding <String> | <Null> 默认 = 'utf8'
mode <Integer> 默认 = 0o666
flag <String> 默认 = 'w'
callback <Function>
1 | fs.writeFile('message.txt', 'Hello Node.js', (err) => { |
注意点
不建议在调用 fs.open() 、 fs.readFile() 或 fs.writeFile()
之前使用 fs.access() 检查一个文件的可访问性。 如此处理会造成紊乱情况,
因为其他进程可能在两个调用之间改变该文件的状态。 作为替代,
用户代码应该直接打开/读取/写入文件,当文件无法访问时再处理错误。