本文共 1020 字,大约阅读时间需要 3 分钟。
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})
翻译自:
node.js删除文件
转载地址:http://eoqgb.baihongyu.com/