node.js删除文件_如何使用Node.js删除文件

node.js删除文件

How do you remove a file from the filesytem using Node.js?

如何使用Node.js从文件系统中删除文件?

Node offers a synchronous method, and an asynchronous method through the fs built-in module.

Node通过fs内置模块提供同步方法和异步方法。

The asynchronous one is fs.unlink().

异步的是fs.unlink()

The synchronous one is fs.unlinkSync().

同步的是fs.unlinkSync()

The difference is simple: the synchronous call will cause your code to block and wait until the file has been removed. The asynchronous one will not block your code, and will call a callback function once the file has been deleted.

区别很简单:同步调用将导致您的代码阻塞并等待,直到文件被删除。 异步代码不会阻塞您的代码,并且在删除文件后将调用回调函数。

Here’s how to use those 2 functions:

这是使用这两个功能的方法:

fs.unlinkSync():

fs.unlinkSync()

const fs = require('fs')

const path = './file.txt'

try {
  fs.unlinkSync(path)
  //file removed
} catch(err) {
  console.error(err)
}

fs.unlink():

fs.unlink()

const fs = require('fs')

const path = './file.txt'

fs.unlink(path, (err) => {
  if (err) {
    console.error(err)
    return
  }

  //file removed
})

翻译自: https://flaviocopes.com/how-to-remove-file-node/

node.js删除文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值