本篇文章给大家介绍一下node.js中的文件夹写入。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

相关推荐:《node js教程》
fs.Dir 是可迭代的目录流的类,fs.Dirent 是遍历 fs.Dir 获得的目录项,可以是文件或目录中的子目录
fs.opendir(path[, options], callback) 打开一个目录,返回 fs.Dir 对象
const fs = require('fs/promises');
async function print(path) {
const dir = await fs.opendir(path);
for await (const dirent of dir) {
console.log(dirent.name);
}
}
print('./').catch(console.error);可以通过 dir.read() 迭代 dir
const fs = require('fs/promises');
async function print(path) {
const dir = await fs.opendir(path);
let dirent = await dir.read();
while (dirent) {
console.log(dirent.name);
dirent = await dir.read();
}
dir.close();
}
print('./').catch(console.error);fs.readdir(path[, options], callback) 读取目录的内容,回调有两个参数 (err, files),其中 files 是目录中的文件名的数组(不包括 '.' 和 '..')
options
const fs = require('fs/promises');
async function print(path) {
const files = await fs.readdir(path);
for (const file of files) {
console.log(file);
}
}
print('./').catch(console.error);fs.mkdir(path[, options], callback) 创建目录
options
mkdir -p 会把不存在的目录创建// 创建 /tmp/a/apple 目录,无论是否存在 /tmp 和 /tmp/a 目录。
fs.mkdir('/tmp/a/apple', { recursive: true }, err => {
if (err) throw err;
});fs.rmdir(path[, options], callback) fs.rmdir 用于删除文件夹
options
const fs = require('fs');
fs.rmdir('./tmp', { recursive: true }, err => console.log);之前 rmdir 只能删除空的文件夹,现在可以连同文件一起删除了
更多编程相关知识,请访问:编程教学!!
以上就是了解一下Node.js中的文件夹写入的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号