[updated]Node.js学习笔记:fs.stat判断文件类型

上一节提到如何用fs.readdir方法读取指定路径的文件列表(见[《写给小白的Node.js学习笔记10:fs模块读取文件列表》,读取出来的结果可以看到,文件夹和文件并没有作任何区分,接下来我们用fs.stat方法对文件类型进行判断。

1
2
3
4
5
6
7
//引入模块,沿用上一节提到的文件夹
const fs = require("fs");
const path ="/Applications/MAMP/htdocs/nodejs/test/";
fs.stat(path, (err, stats) => {
console.log("It is a folder: " + stats.isDirectory());
console.log("It is a file: " + stats.isFile());
});

返回结果
输出结果

fs.stat方法会读取文件,并返回一个对象fs.statsstats对象有一下判断方法,可以判断文件类型
fs.stats方法


[2018/09/03更新]