网站建设哪家好服务,集团网站建设的要求,搜狗竞价绑定网站要求,黄山小程序开发作者#xff1a;CSDN-PleaSure乐事 欢迎大家阅读我的博客 希望大家喜欢 使用环境#xff1a;Vscode 本文代码都经由博主PleaSure乐事实操后得出#xff0c;可以放心使用。 1.FileSystem介绍
Node.js 的 fs#xff08;filesystem#xff09;模块是一个核心模块#xff0c… 作者CSDN-PleaSure乐事 欢迎大家阅读我的博客 希望大家喜欢 使用环境Vscode 本文代码都经由博主PleaSure乐事实操后得出可以放心使用。 1.FileSystem介绍
Node.js 的 fsfilesystem模块是一个核心模块提供了与文件系统进行交互的功能。它允许你读取、写入、删除和管理文件和目录。fs 模块提供了同步和异步两种方式来执行文件操作使得编写者可以根据具体需求选择合适的方法。 2.FileSystem基本操作介绍
在正式进行FileSystem前我们需要创建一个js文件并对fs模块进行导入
//方法一
import fs from fs
//方法二
const fs require(fs);
2.1创建目录
如果我们想要创建目录我们仅需要使用mkdir即可fs.promises.mkdir会进行目录创建其中recursive是用来控制是否允许创建多级目录若设置为true则允许创建多级目录反之false则不能创建。
const createDir async (path) {try {await fs.promises.mkdir(path, { recursive: true }) // recursive: true 允许递归创建多级目录console.log(目录创建成功)} catch (err) {console.error(目录创建失败: ${err})}}
let dir PleaSure/csdn/
createDir(dir) //创建多级目录
如下图所示我们已经成功创建了一个多级目录 2.2写入文件
如果我们要写入文件我们需要使用writeFile来处理fs.promises.writeFile会进行文件写入。但是需要注意我们最好添加path.join来处理不同电脑间路径不一致的情况在 Windows 系统中路径分隔符通常是反斜杠\而在 Unix 和类 Unix 系统如 Linux 和 macOS中路径分隔符是正斜杠/。
const writeFile async (filePath, content) {try {await fs.promises.writeFile(filePath, content);console.log(文件写入成功);} catch (err) {console.error(文件写入失败: ${err});}
};// 定义目录和文件路径
let dir PleaSure/csdn/;
let name web.txt;let filePath path.join(dir, name); // 使用 path.join 来处理路径// 创建目录
createDir(dir).then(() {// 写入文件writeFile(filePath, content);
});
但是需要注意这里的写入文件是会覆盖的 2.3追加文件 追加文件则直接使用appendFile即可fs.promises.appendFile会进行文件追加内容将被写在文件最后
const appendToFile async (filePath, content) {try {await fs.promises.appendFile(filePath, content);console.log(内容追加成功);} catch (err) {console.error(内容追加失败: ${err});}};// 定义目录和文件路径let dir PleaSure/csdn/;let name web.txt;let filePath path.join(dir, name); // 使用 path.join 来处理路径// 创建目录createDir(dir).then(() {// 追加内容到文件appendToFile(filePath, pleasure\n); // 追加内容并换行}); 2.4读取文件
如果想要读取文件我们增加readFIle即可fs.promises.readFile会完成文件读取输出文件内容
const readFile async (filePath) {try {const data await fs.promises.readFile(filePath, utf8);console.log(文件内容:);console.log(data);} catch (err) {console.error(文件读取失败: ${err});}}; 2.5检查文件或目录是否存在
我们可以使用access来判断文件或目录是否存在fs.promises.access会完成检查从而判断下一步操作怎么办例如创建新的目录等等
const checkDirExists async (dirPath) {try {await fs.promises.access(dirPath, fs.constants.F_OK);console.log(目录 ${dirPath} 存在);return true;} catch (err) {console.log(目录 ${dirPath} 不存在);return false;}
};
检查目录或文件是否存在可以帮助我们判断是否需要进行新建文件或目录操作防止有重名等情况的发生
checkDirExists(dir).then((exists) {if (!exists) {// 如果目录不存在则创建目录return createDir(dir);}return Promise.resolve(); // 目录已存在直接返回一个 resolved 的 Promise
}).then(() {// 写入文件return writeFile(filePath, content);
}).then(() {// 追加内容到文件return appendToFile(filePath, pleasure\n); // 追加内容并换行
}).then(() {// 读取文件return readFile(filePath);
});
2.6获取目录详细信息
使用stat可以来获取目录详细信息例如创建时间、修改时间、大小等我们使用birthtime等即可完成相应信息的调取
const getDirInfo async (dirPath) {try {const stats await fs.promises.stat(dirPath);if (stats.isDirectory()) {console.log(目录 ${dirPath} 的详细信息:);console.log(- 大小: ${stats.size} 字节);console.log(- 创建时间: ${stats.birthtime});console.log(- 修改时间: ${stats.mtime});console.log(- 访问时间: ${stats.atime});console.log(- 是否为目录: ${stats.isDirectory()});console.log(- 是否为文件: ${stats.isFile()});console.log(- 是否为符号链接: ${stats.isSymbolicLink()});} else {console.log(${dirPath} 不是一个目录);}} catch (err) {console.error(无法获取目录 ${dirPath} 的信息: ${err});}
};最终可以得到这样的效果 2.7文件或目录重命名
入伏哦我们想要完成目录或文件重命名我们可以使用rename就可以达到效果fs.promises.rename为完整写法
const renameFileOrDir async (oldPath, newPath) {try {await fs.promises.rename(oldPath, newPath);console.log(重命名成功: ${oldPath} - ${newPath});} catch (err) {console.error(重命名失败: ${err});}};
此时我们可以看到文件名已经被修改了 2.8删除文件
若想要删除文件我们直接调用unlink即可完成fs.promises.unlink会帮助进行文件删除
const deleteFile async (filePath) {try {await fs.promises.unlink(filePath);console.log(文件删除成功: ${filePath});} catch (err) {console.error(文件删除失败: ${err});}
};
此时文件也已经被顺利删除了。
2.9删除目录
如果我们想要删除整个目录可以使用 fs.promises.rmdir 方法来删除空目录或者使用 fs.promises.rm 方法来删除非空目录。fs.promises.rm 方法提供了更多的选项比如递归删除目录及其内容。
const deleteDir async (dirPath) {try {await fs.promises.rm(dirPath, { recursive: true, force: true });console.log(目录删除成功: ${dirPath});} catch (err) {console.error(目录删除失败: ${err});}
};此时想要删除的目录也已经被删掉了。
3.最终结果
此时查看控制台可以看到如下结果我们从头到尾从创建到删除的每一步都顺利执行 完成每一步操作的代码都可以在文章中获得若有需要欢迎收藏文章反复擦写感谢您的阅读 作者CSDN-PleaSure乐事 希望我的博客对您有帮助也希望在对您有帮助时您可以为我留下点赞收藏与关注这对我真的很重要谢谢