在 nodeJs 环境中封装读取目录中的所有子目录和子文件的方法
要求:给定一个路径,返回该路径下的所有子目录和子文件,每个目录或文件都是一个对象,返回的对象结构如下
属性名 | 类型 | 说明 |
---|---|---|
name | String | 文件名 |
filename | String | 文件路径 |
ext | String | 后缀名;目录则为空字符串 |
isFile | Boolean | 是否是一个文件 |
size | Number | 文件大小 |
createTime | Date | 日期对象,创建时间 |
updateTime | Date | 日期对象,修改时间 |
getChilden | Function | 得到目录的所有子文件对象数组,文件则返回空数组 |
getContent | Function | 返回读取文件内容;目录则返回 null |
- 可以将读取文件的部分封装成一个类,通过调用原型上的属性返回文件结构对象;
const fs = require("fs");
const path = require("path");
//定义一个File类,
class File {
//静态方法 getFileProps 通过对传入的路径处理 给实例对象上添加属性
static async getFileProps( filename ){
const stat = await fs.stat(filename);
const name = path.basename(