`模块工具包`分类下的文章

技术

文件读取

###

普通复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 复制文件 (异步,覆盖原文件)
* @param {*} source 源文件
* @param {*} dest 目标文件
*/
function mycopy(path1, path2) {
fs.readFile(path1, function(err, data) {
if (err) {
console.log(err);
} else {
var data1 = data.toString();
fs.writeFile(path2, data1, function(err) {
if (err) {
console.log(err);
} else {
console.log("复制成功");
}
})
}
})
}

pipe复制文件

例子:

1
2
3
4
5
6
7
8
9

// 1.引入模块
let fs =require('fs');
var request = require('request');
//2.创建读入流
let rs = fs.createReadStream('D:/Pictures/Saved Pictures/testSP.mp4'); //要读取视频的位置
let ws = fs.createWriteStream('testSP.mp4'); //视屏要写入的位置
//创建管道
rs.pipe(ws);

详细

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

// 1.引入模块
let fs =require('fs');
//2.创建读入流
let rs = fs.createReadStream('D:/Pictures/Saved Pictures/testSP.mp4'); //要读取视频的位置
let ws = fs.createWriteStream('testSP.mp4'); //视屏要写入的位置,我这里是默认的项目文件夹下的

//3.监听流的打开和关闭
ws.once('open' ,()=>{
console.log("读入通道打开");
});
ws.once('close' ,()=>{
console.log("读入通道以关闭");
});

rs.once('open' ,()=>{
console.log("写出通道已打开");
});
rs.once('close' ,()=>{
console.log("写出通道已关闭");
});
//4.绑定data
rs.on("data", (data)=>{
ws.write(data);
});

参考:

https://blog.csdn.net/qq_41542894/article/details/82291555

https://www.jianshu.com/p/21c7db836836

文件操作
https://www.jianshu.com/p/482a2d272a21?utm_source=oschina-app

文件的同步和异步写入操作
https://blog.csdn.net/qq_41542894/article/details/82229607

官网

http://nodejs.cn/api/stream.html#stream_event_pipe

阅读剩下更多

默认配图
技术

node递归文件夹

###
TODO

方案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
35
36
37
38
39
40
41
42
var fs = require('fs');
var path = require('path');

//解析需要遍历的文件夹,我这以E盘根目录为例
var filePath = path.resolve('E:');

//调用文件遍历方法
fileDisplay(filePath);

/**
* 文件遍历方法
* @param filePath 需要遍历的文件路径
*/
function fileDisplay(filePath){
//根据文件路径读取文件,返回文件列表
fs.readdir(filePath,function(err,files){
if(err){
console.warn(err)
}else{
//遍历读取到的文件列表
files.forEach(function(filename){
//获取当前文件的绝对路径
var filedir = path.join(filePath,filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir,function(eror,stats){
if(eror){
console.warn('获取文件stats失败');
}else{
var isFile = stats.isFile();//是文件
var isDir = stats.isDirectory();//是文件夹
if(isFile){
console.log(filedir);
}
if(isDir){
fileDisplay(filedir);//递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
})
});
}
});
}
1
2
3
4
5
6
如果碰到有中文不能解析的html,这样写
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var myHtml = fs.readFileSync("index.html");
var myHtml2 = iconv.decode(myHtml, 'gbk');
console.log(myHtml2);

说明:异步的方式读取

方案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

var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;

function readFileList(dir, filesList = []) {
//同步读取
const files = fs.readdirSync(dir);
console.log(files);
files.forEach((item, index) => {
var fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
filesList.push(fullPath);
}
});
return filesList;
}

var filesList = [];
readFileList(__dirname,filesList);
console.log(filesList);

说明:同步的方式读取

参考

https://blog.csdn.net/younglao/article/details/77046830

https://blog.csdn.net/liyazhen2011/article/details/87882180

阅读剩下更多

默认配图
返回顶部