fs.unlinkSync()方法用于从文件系统中同步删除文件或符号链接。此函数不适用于目录,因此建议使用fs.rmdir()删除目录。
用法:
fs.unlinkSync( path )
参数:此方法接受上面提到的和下面描述的一个参数:
- path:它是一个字符串,Buffer或URL,代表必须删除的文件或符号链接。
以下示例说明了Node.js中的fs.unlinkSync()方法:
范例1:本示例从文件系统中删除文件。
// Node.js program to demonstrate the
// fs.unlinkSync() method
// Import the filesystem module
const fs = require('fs');
// Get the files in current diectory
// before deletion
getFilesInDirectory();
// Delete readme.md
fs.unlinkSync("readme.md");
console.log("\nFile readme.md is deleted");
// Get the files in current diectory
// after deletion
getFilesInDirectory();
// Function to get current filenames
// in directory with specific extension
function getFilesInDirectory() {
console.log("\nFiles present in directory:");
let files = fs.readdirSync(__dirname);
files.forEach(file => {
console.log(file);
});
}
输出:
Files present in directory: index.html index.js package.json readme.md File readme.md is deleted Files present in directory: index.html index.js package.json
范例2:本示例从文件系统中删除符号链接。
// Node.js program to demonstrate the
// fs.unlinkSync() method
// Import the filesystem module
const fs = require('fs');
// Creating symlink to file
fs.symlinkSync(__dirname + "\\readme.md", "symlinkToReadme");
console.log("\nSymbolic link to readme.md created");
// Function to get current filenames
// in directory with specific extension
getFilesInDirectory();
// Deleting symbolic link to readme.md
fs.unlinkSync("symlinkToReadme");
console.log("\nSymbolic link to readme.md deleted")
getFilesInDirectory();
// Function to get current filenames
// in directory with specific extension
function getFilesInDirectory() {
console.log("\nFiles present in directory:");
let files = fs.readdirSync(__dirname);
files.forEach(file => {
console.log(file);
});
}
输出:
Symbolic link to readme.md created Files present in directory: index.html index.js package.json readme.md symlinkToReadme Symbolic link to readme.md deleted Files present in directory: index.html index.js package.json readme.md
参考: https://nodejs.org/api/fs.html#fs_fs_unlinksync_path
相关用法
- Node.js GM minify()用法及代码示例
- Node.js GM magnify()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM drawRectangle()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM modulate()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.unlinkSync() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。