简单文件的读写
文件读操作
- 异步
fs.readFile(path[, options], callback)
- 同步
fs.readFileSync(path[, options])
var fs = require('fs')
fs.readFile('./data/a.txt',function(err,data){
if(err){
console.log('文件读取失败')
}
else{
console.log(data.toString())
}
})
文件写操作
- 异步
fs.writeFile(path , data[, options], callback)
- 同步
fs.writeFileSync(path , data[, options])
- path 文件的路径、options 可选参数、callback回调函数
- flag : r 只读、w 可写、a 追加
var fs = require('fs')
fs.writeFile("hello3.txt","这是通过writeFile写入的内容",{flag:"r+"} , function (err) {
if(err){
console.log(err)
}else{
console.log("写入成功~~~")
}
})