要一直走下去

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以下方法都是异步的

引入文件系统模块:

fs.readFile(path[, options], callback) 读取文件
  path :文件路径
  callback: 回调函数
  err:错误
  data :读取的数据,不写utf8,data是Buffer字节数组
fs.writeFile(file, data[, options], callback)  写入文件 
  file :文件名或文件描述符。
  data :写入的数据
  options :
    encoding: 写⼊字符串的编码格式 默认值: 'utf8' 。
    mode :文件模式(权限) 默认值: 0o666 。
    flag :默认值: 'w' 。
  callback 回调函数
  err
fs.appendFile(path, data[, options], callback) 追加数据到文件
  path:文件名或文件描述符。
  data: 追加的数据
  options 
    encoding:  写入字符串的编码格式 默认值: 'utf8' 。
    mode :文件模式(权限) 默认值: 0o666 。
    flag :默认值: 'a' 。
  callback 回调函数
  err
fs.stat(path[, options], callback) 获取文件信息,判断文件状态(是文件还是文件夹)
  path
  options
    bigint 返回的 fs.Stats 对象中的数值是否应为 bigint 型。默认值: false 。
  callback
  err
  stats <fs.Stats> 文件信息

 

fs.rename(oldPath, newPath, callback) 重命名文件
  oldPath:旧文件路径名字
  newPath: 新文件路径名字
  callback 回调函数
  err
fs.unlink(path, callback) 删除文件
  path
  callback
  err
fs.mkdir(path[, options], callback) 创建文件夹
  path
  options 
    recursive 是否递归创建 默认值: false 。
    mode 文件模式(权限)Windows 上不支持。默认值: 0o777 。
  callback
  err
fs.readdir (path[, options], callback) 读取文件夹
  path
  options
    encoding 默认值: 'utf8' 。
    withFileTypes 默认值: false 。
  callback
  err
  files <string[]> | <buffffer[]> | <fs.Dirent[]>
fs.rmdir(path[, options], callback) 删除文件夹
  path
  options
    maxRetries 重试次数。出现这类错误 EBUSY 、 EMFILE 、 ENFILE 、 ENOTEMPTY 或者EPERM ,每⼀个重试会根据设置的重试间隔重试操作。如果 recursive 不为true则忽略. 默认值: 0。
    retryDelay 重试的间隔,如果 recursive 不为true则忽略. 默认值: 100 。
    recursive 如果为 true ,则执行递归的目录删除。在递归模式中,如果 path 不存在则不报告错误,并且在失败时重试操作。默认值: false 。
  callback
  err
 
 
监听文件变化使用chokidar 
安装chokidar:npm install chokidar --save-dev
Chokidar.watch(path,[options])

 

 

posted on 2022-07-14 21:09  要一直走下去  阅读(121)  评论(0)    收藏  举报