文件读取大部分都是拥有异步和同步的两个接口
普通的文件读取
// 同步读取 const fs = require('fs') try { const data = fs.readFileSync('./hello.txt', 'utf8') }catch(err) { throw err } // 异步读取 fs.readFile('./hello.txt','utf8', function(err, data) { if(err) throw err console.log('读取成功:' + data) })
以流的方式读取
const fs = require('fs') const readStream = fs.createReadStream('./hello.txt', 'utf8') readStream.on('data', function(chunk){ console.log('读取数据:'+ chunk) }).on('error', function(err) { console.log(err.message) }).on('end', function() { console.log('没有数据了') }).on('close', function() { console.log('已经关闭') })
简单写入
const fs = require('fs') // 同步写入 try { fs.writeFileSync('./hello.txt', 'hello node', utf8) }catch(err) { throw err } // 异步写入 fs.writeFile('./hello.txt','hello node', 'utf8', (err,data) => { if(err) throw err console.log(data) })
以流的方式写入
const fs = require('fs') const writeStream = fs.createWriteStream('./hello.txt', 'hello node', 'utf8') // 在end() 后触发 writeStream.on('close', function(){ console.log('关闭') }) writeStream.write('hello') writeStream.write('node') writeStream.end()
查看某个文件是否存在(也可以用来检测时候有某个文件的权限)
const fs = require('fs') fs.access('./hello.txt', function(err){ if(err) throw err console.log('该文件存在,可以访问这个文件') })
创建删除目录
const fs = require('fs') // 如果目录存在报错 // 异步 fs.mkdir('./abc', function(err) { if(err) throw err console.log('创建成功') }) // 同步 fs.mkdirSync('./abc') //删除目录 fs.rmdir('./abc',function(err) { if(err) throw err console.log('删除成功') }) // 同步 fs.rmdirSync('./abc')
创建临时目录
const fs = require('fs') fs.mkdtemp('./', function(err, folder) { if(err) throw err console.log('创建的临时文件夹' + folder) } )
删除某个文件
const fs = require('fs') // 异步 fs.unlink('./hello.txt', function(err){ if(err) throw err console.log('文件删除成功') }) // 同步 fs.unlinkSync('./hello.txt')
文件属性
const fs = require('fs') //异步 fs.stat('./hello.txt', function(err, stats){ if(err) throw console.log('文件大小', stats.size) console.log('创建时间', stats.birthtime) console.log('访问时间', stats.atime) console.log('修改时间', stats.mtime) }) // 同步 const stats = fs.statSync('./hello.txt') /* stats 对象中创建的属性 dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atime: Mon, 10 Oct 2011 23:24:11 GMT, // 访问时间 mtime: Mon, 10 Oct 2011 23:24:11 GMT, // 文件内容修改时间 ctime: Mon, 10 Oct 2011 23:24:11 GMT, // 文件状态修改时间 birthtime: Mon, 10 Oct 2011 23:24:11 GMT // 创建时间 stats对象常见的方法 stats.isFile() 是否是文件 stats.isDirectory() 是否是目录 stats.isSocket() 是否是socket 文件 */
监听文件变化
const fs = require('fs') //fs.watch() 和 fs.watchFile() //fs.watchFile() 是通过轮询的方式, const option = { persistent: true, interval: 2000 } fs.watchFile('./hello.txt', option, function(curr, prv){ console.log('修改时间'+ curr.mtime) }) // if you want to be notified when the file was modified, not just accessed, you need to compare curr.mtime and prev.mtime
遍历目录
const fs = require('fs') const path = require('path') //fs.readdirSync() 只能读取一层目录 function getAllFile(dir) { let arr = [] const files = fs.readdirSync(dir,'utf8') files.forEach(file => { const filePath = path.resolve(dir, file) const stats = fs.statSync(file) if(stats.isFile()){ arr.push(file) }else if(stats.isDirectory()){ arr = arr.concat(getAllFile(file)) } }) return arr } const files = getAllFile('./abc') console.log(files)
重命名
const fs = require('fs') // 异步 fs.rename('./abc','./abcd', function(err) { if(err) throw err console.log('重命名成功') }) // 同步 fs.renameSync('./abcd', './abc')
还有一些其他的
//追加文件内容 // fs.appendFile() // 打开文件 // fs.open() // 读取文件 // fs.read() // 文件内容截取 // fs.truncate() // 修改文件属性 // fs.utimes() // 等等....
学模块步骤
来源:https://www.cnblogs.com/ADong4031/p/12330320.html