1. node path 路径
var path = require("path");
var str= "c:\\acicv\\com\\index.html";
console.log(path.basename(str,".html")); //返回的是一个路径的文件名
console.log(__filename); //返回当前文件的路径
console.log(path.delimiter)// windsow的系统
console.log(path.posix.delimiter); //linux系统
console.log(path.win32.delimiter); //linux系统
console.log(path.dirname(str))//返回一个文件的文件夹
console.log(path.extname(str));//返回文件的扩展名
2.fs 文件系统
fs.watch()监控类型
const fs = require("fs");
写法一
//启动监听文件夹
let watcher = fs.watch(__dirname);
watcher.on('change',function(evenType,fileName){
console.log('change:', evenType, fileName); //evenType事件类型 fileName 文件名
})
//10s之后关闭文件监听
setTimeout(function(){
watcher.close();
},10000);
返回 change: change 1.js
写法二
let watcher = fs.watch(__dirname,function(evenType,fileName){
console.log('change:', evenType, fileName); //evenType事件类型 fileName 文件名
});
fs.readFileSync(path[,options]);//同步读取文件 仅适用于读取小文件
const fs = require("fs");
//读取当前文件的内容 readFileSync是同步方法,当node执行到此行代码的时候整个程序等待文件读取完毕,再往下执行
let fileContert = fs.readFileSync(__filename,{
encoding:'utf8'
})
console.log(fileContert)
fs.readFile(path[,options]) //异步读取文件 仅适用于读取小文件
const fs = require("fs");
let indexJSFileName=path.join(__dirname,'index.html'); //拼接文件夹和文件 组成正确的文件路径
let fileContert = fs.readFile(indexJSFileName,{
encoding:'utf8'
},function(err,data){ //文件读取完毕执行回调函数
if(err){
console.log("读取异常",err)
}
console.log(data)
})
console.log(‘主线程继续执行。。。。’)
读取大文件
const fs = require("fs")
const path=require("path");
let fileName =path.join(__diirname,'a.mp4');
//创建一个可读的流
let stream = fs.creatReadStream(fileName,{encoding:'utf8'}); //返回的数据流是字符串 默认是二进制数字.
//当流的管道建立并打开的时候 触发OPEN事件
stream.on('open',function(fd){
console.log(建立管道并打开)
})
let i = 0;
stream.on('data',function(trunk){ //当数据流过来直接触发data事件
})
stream.on('end',function(){ //数据读取完毕
})
const fs = require("fs");
const path = require('path');
let aFilePath = path.join(__dirname,'a1.txt');
//异步写入到文件中一个字符串
fs.writeFile(aFilePath,'你好',function(err){
if(err){
console.log(err)
}
})
//同步写入到一个文件中
fs.writeFileSync(aFilePath,'你好2');
node写入文件流
const fs = require("fs");
const path = require('path');
let aFilePath = path.join(__dirname,'a1.txt');
//要用可写流,往txt里面写入100个个字符串
let stream = fs.createWriteStream(aFilePath ,{flags:'a'});
stream.on("open",function(fd){
console.log('可写流打开',fd)
})
stream.on("close",function(){
console.log('可写流关闭')
})
//往可写流里面写入数据
for(let i=0;i<100;i++){
stream.write('aaaaassscaaaaacccaaa')
}
stream.end('结尾!')
可读流读取数据 可写流写入数据
const fs = require("fs");
const path = require('path');
let aFilePath = path.join(__dirname,'a1.txt');
let bFilePath = path.join(__dirname,'b1.txt');
//要用可写流,往txt里面写入100个个字符串
let rs = fs.createReadStream(aFilePath);
let ws = fs.createWriteStream(bFilePath);
rs.on("open",function(fd){
console.log('开始读取文件内容',fd)
})
rs.on("close",function(){
console.log('读取结束')
})
rs.on('data',function(trunk){
if(ws.write.trunk == false){
//缓冲区满了不能写入了,让可读流暂时停止读取数据
rs.pause();
}
})
ws.on('drain',function(){
//可写流可以继续写入,让可读流继续读数据
rs.resume()
})
rs.pipe(ws);//可读流和可写流之间建立管道直接读取 等同于上面
const fs = require("fs");
const path = require('path');
//读取一个路径返回 文件及文件夹
fs.readdir(__dirname,function(err,files){
files.array.forEach(item => {
//判断是文件还是文件夹
fs.stat(path.join(__dirname,item),function(err,stat){
stat.isFile()?console.log('file:',item):console.log('dir:',item)
})
});
})