Node-文件操作(中)- 文件状态、promise版本封装

获取文件信息

1
2
3
const fs = require('fs');
fs.stat(path,callback);

文件目录

readdir 只能读取一级目录

1. readdir 方法初步使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const fs = require('fs');
const path = require('path');
fs.readdir(rootDir, (err, data) => {
if (err) {
throw err;
}
for(let name of files) {
const tmpPath = path.join(rootDir, name);
fs.stat(tmpPath, (err, stats) => {
if (err) {
throw err;
}
if (stats.isFile() && path.extname(tmpPath) === '.cmd') {
console.log(tempPath);
}
});
}
});

2. 把 1 的代码封装成一个方法,得到一个目录下所有后缀名的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const fs = require('fs');
const path = require('path');
const rootDir = './data';
getPathsByFilter(rootDir, '.txt', (err, paths) => {
if (err) {
throw err;
}
console.log(paths);
});
// 封装成一个方法,得到一个目录下所有后缀名的文件
function getPathsByFilter(rootDir, extName, callback) {
fs.readdir(rootDir, (err, files) => {
if (err) {
return callback(err);
}
const paths = [];
let count = 0;
for(let name of files) {
const tmpPath = path.join(rootDir, name);
fs.stat(tmpPath, (err, stats) => {
if (err) {
return callback(err);
}
if (stats.isFile() && path.extname(tmpPath) === extName) {
paths.push(tmpPath);
}
count++;
if (count === files.length) {
callback(null, paths);
}
});
}
});
}

3. 把 2 代码块得到的所有文件都读出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs');
const path = require('path');
const rootDir = './data';
getPathsByFilter(rootDir, '.txt', (err, paths) => {
if (err) {
throw err;
}
// 读文件
Promise.all(paths.map(p => readFile(p)))
.then(data => {
data.forEach(d => console.log(d.toString()));
})
.catch(err => {
throw err;
});
});
// 封装成一个方法,得到一个目录下所有后缀名的文件
function getPathsByFilter(rootDir, extName, callback) {
fs.readdir(rootDir, (err, files) => {
if (err) {
return callback(err);
}
const paths = [];
let count = 0;
for(let name of files) {
const tmpPath = path.join(rootDir, name);
fs.stat(tmpPath, (err, stats) => {
if (err) {
return callback(err);
}
if (stats.isFile() && path.extname(tmpPath) === extName) {
paths.push(tmpPath);
}
count++;
if (count === files.length) {
callback(null, paths);
}
});
}
});
}
// 读文件的方法
function readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}

这样封装的 getPathsByFilter() 方法不是好的 API,要用 Promise, 都封装成 Promise 版本

4. 把 3 代码优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs');
const path = require('path');
const rootDir = './data';
getPathsByFilter(rootDir, '.txt')
.then(paths => {
return Promise.all(paths.map(p => readFile(p)))
})
.then(data => {
data.forEach(d => console.log(d.toString()));
})
.catch(err => {
throw err;
});
// 封装成一个方法,得到一个目录下所有后缀名的文件
function getPathsByFilter(rootDir, extName) {
return new Promise((resolve, reject) => {
fs.readdir(rootDir, (err, files) => {
if (err) {
return reject(err);
}
const paths = [];
let count = 0;
for(let name of files) {
const tmpPath = path.join(rootDir, name);
fs.stat(tmpPath, (err, stats) => {
if (err) {
return reject(err);
}
if (stats.isFile() && path.extname(tmpPath) === extName) {
paths.push(tmpPath);
}
count++;
if (count === files.length) {
resolve(paths);
}
});
}
});
});
}
// 读文件的方法
function readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}

5. 终极 promise 版本的指定文件目录,读取指定后缀名的所有文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs');
const path = require('path');
const rootDir = './data';
getPathsByFilter(rootDir, '.txt')
.then(paths => {
return Promise.all(paths.map(p => readFile(p)))
})
.then(data => {
data.forEach(d => console.log(d.toString()))
})
.catch(err => {
throw err
})
function getPathsByFilter(rootDir, extName) {
return new Promise((resolve, reject) => {
readdir(rootDir).then(files => {
const paths = []
let count = 0
for (let name of files) {
const tmpPath = path.join(rootDir, name)
stat(tmpPath).then(stats => {
// 判断是否是文件并且后缀名是指定的 .txt
if (stats.isFile() && path.extname(tmpPath) === extName) {
paths.push(tmpPath)
}
count++
if (count === files.length) {
resolve(paths)
}
})
.catch(err => {
// 主动在当前代码块抛出异常
Promise.reject(err)
})
}
})
.catch(err => {
reject(err)
})
})
}
// 文件目录操作
function readdir(dir) {
return new Promise((resolve, reject) => {
fs.readdir(dir, (err, files) => {
if (err) {
return reject(err)
}
resolve(files)
})
})
}
// 读文件的方法
function readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
return reject(err)
}
resolve(data)
})
})
}
// 获取文件状态
function stat(filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if (err) {
return reject(err)
}
resolve(stats)
})
})
}
感谢您的支持!