fs
(File System)模块是 Node.js 的核心模块之一,用于处理文件系统的操作,包括文件的读取、写入、删除、重命名等。它提供了同步和异步两种操作方式,适用于不同的场景。
1. 前置知识
1.1 文件系统
文件系统是操作系统用于管理文件和目录的一种机制。Node.js 通过 fs
模块提供了对文件系统的访问能力。
1.2 同步与异步
- 同步操作:阻塞代码执行,直到操作完成。
- 异步操作:非阻塞,通过回调函数、Promise 或
async/await
处理结果。
1.3 文件路径
- 相对路径:相对于当前工作目录的路径。
- 绝对路径:从根目录开始的完整路径。
- 路径处理:可以使用
path
模块处理路径相关问题。
2. 概念与意义
2.1 概念
fs
模块提供了对文件系统的操作接口,包括:
- 文件的读取和写入。
- 目录的创建和删除。
- 文件属性的获取和修改。
- 文件监视(watch)。
2.2 意义
- 文件操作:读写文件是许多应用的基础功能,如配置文件读取、日志记录等。
- 异步支持:Node.js 的非阻塞 I/O 模型使得文件操作不会阻塞主线程,适合高并发场景。
- 跨平台:
fs
模块在不同操作系统上表现一致,提供了跨平台的文件操作能力。
3. 基本操作与用法
3.1 引入 fs
模块
const fs = require('fs');
3.2 文件读取
异步读取
fs.readFile('example.txt', 'utf8', (err, data) => {if (err) {console.error('Failed to read file:', err);} else {console.log('File content:', data);}
});
同步读取
try {const data = fs.readFileSync('example.txt', 'utf8');console.log('File content:', data);
} catch (err) {console.error('Failed to read file:', err);
}
3.3 文件写入
异步写入
fs.writeFile('example.txt', 'Hello, world!', 'utf8', (err) => {if (err) {console.error('Failed to write file:', err);} else {console.log('File written successfully');}
});
同步写入
try {fs.writeFileSync('example.txt', 'Hello, world!', 'utf8');console.log('File written successfully');
} catch (err) {console.error('Failed to write file:', err);
}
3.4 文件追加
异步追加
fs.appendFile('example.txt', '\nNew content', 'utf8', (err) => {if (err) {console.error('Failed to append file:', err);} else {console.log('Content appended successfully');}
});
同步追加
try {fs.appendFileSync('example.txt', '\nNew content', 'utf8');console.log('Content appended successfully');
} catch (err) {console.error('Failed to append file:', err);
}
3.5 文件删除
异步删除
fs.unlink('example.txt', (err) => {if (err) {console.error('Failed to delete file:', err);} else {console.log('File deleted successfully');}
});
同步删除
try {fs.unlinkSync('example.txt');console.log('File deleted successfully');
} catch (err) {console.error('Failed to delete file:', err);
}
3.6 文件重命名
异步重命名
fs.rename('old.txt', 'new.txt', (err) => {if (err) {console.error('Failed to rename file:', err);} else {console.log('File renamed successfully');}
});
同步重命名
try {fs.renameSync('old.txt', 'new.txt');console.log('File renamed successfully');
} catch (err) {console.error('Failed to rename file:', err);
}
3.7 目录操作
创建目录
fs.mkdir('newDir', (err) => {if (err) {console.error('Failed to create directory:', err);} else {console.log('Directory created successfully');}
});
删除目录
fs.rmdir('newDir', (err) => {if (err) {console.error('Failed to delete directory:', err);} else {console.log('Directory deleted successfully');}
});
3.8 文件属性
获取文件信息
fs.stat('example.txt', (err, stats) => {if (err) {console.error('Failed to get file stats:', err);} else {console.log('File size:', stats.size);console.log('Is file?', stats.isFile());console.log('Is directory?', stats.isDirectory());}
});
4. 优缺点
4.1 优点
- 功能丰富:支持文件的读写、目录操作、文件监视等。
- 异步支持:非阻塞 I/O 适合高并发场景。
- 跨平台:在不同操作系统上表现一致。
- 灵活性:提供同步和异步两种操作方式。
4.2 缺点
- 回调地狱:异步操作可能导致回调嵌套,降低代码可读性。
- 错误处理:需要手动处理错误,否则可能导致程序崩溃。
- 性能问题:同步操作会阻塞主线程,不适合高性能场景。
5. 示例:读取目录下的所有文件
以下是一个读取目录下所有文件的示例:
const fs = require('fs');
const path = require('path');const listFiles = (dir) => {fs.readdir(dir, (err, files) => {if (err) {console.error('Failed to read directory:', err);} else {files.forEach((file) => {const filePath = path.join(dir, file);fs.stat(filePath, (err, stats) => {if (err) {console.error('Failed to get file stats:', err);} else {if (stats.isFile()) {console.log('File:', filePath);} else if (stats.isDirectory()) {listFiles(filePath); // 递归读取子目录}}});});}});
};listFiles('./');
6. 总结
fs
模块 是 Node.js 中处理文件系统的核心工具。- 优点:功能丰富、异步支持、跨平台。
- 缺点:回调地狱、错误处理复杂、同步操作性能问题。
- 适用场景:文件读写、目录操作、配置文件处理、日志记录等。
通过合理使用 fs
模块,可以高效地处理文件系统操作,满足各种应用需求。