1.Buffer(缓冲区)
//Buffer简单示例
//1.创建指定容量的buffer
var buf1 = new Buffer(10);
console.log(buf1); //Result: <Buffer 88 41 66 ed a0 02 00 00 00 00>
//2.根据数组创建buffer
var buf2 = new Buffer([1,2,3,4,5]);
console.log(buf2); //Result: <Buffer 01 02 03 04 05>
//3.根据字符串创建buffer
var buf3 = new Buffer('string','utf8');
console.log(buf3); //Result:<Buffer 73 74 72 69 6e 67>
buf3 = new Buffer('string','hex'); //16进制
console.log(buf3); //Result:<Buffer >
//Node.js buffer支持的编码utf8/ascii/utf16le/ucs2/base64/binary/hex
//判断编码是否被支持
console.log(Buffer.isEncoding('ascii')); //Result: true
//判断对象是否是buffer的示例
console.log(Buffer.isBuffer(buf3)); //Resule: true
//获取buffer的字节长度,与编码有关
console.log(Buffer.byteLength('string','hex')); //Resule:3
//连接bufffer,可选参数为最大长度
console.log(Buffer.concat([buf1,buf2,buf3])); //Result:<Buffer f0 a4 99 15 75 01 00 00 d0 a6 01 02 03 04 05>
//将字符串写入buffer
buf1.write('string',0,6,'utf8');
console.log(buf1); //Result:<Buffer 73 74 72 69 6e 67 01 00 00 00>
//将buffer打印出来
console.log(buf1.toString('utf8',0,6)); //Result: string
//将buffer转换为json对象
console.log(buf1.toJSON()); //Result: { type: 'Buffer',
//data: [ 115, 116, 114, 105, 110, 103, 0, 0, 0, 0 ] }
//buffer复制
var buf4 = new Buffer(10);
var buf5 = new Buffer('string','utf8');
//将buf5中的第1位开始,第3位结束的内容复制到buf4上的第2位开始的位置
buf5.copy(buf4,2,1,3); //2是buf4的起点,1/3是buf5的起始点/结束点
console.log(buf4.toString('utf8',2,4)); //Result:tr
2.FileSystem(文件系统) / Stream(流) /Path(路径处理)
1)FileSystem 与Stream
var fs = require('fs');
var newFile = './核心库/tpl1.txt';
//追加指定内容到文件
fs.appendFile(newFile,'No.2 New Content','utf8',function (err) {
if(err){
console.log(err);
}
});
//打开文件和关闭文件
fs.open(newFile,'w',0666,function (err,fd) { //打开文件时找出fd,后续用于关闭
if(err){
throw err;
}
//关闭文件时,第一个参数是文件句柄
fs.close(fd,function (err) {
if(err){
console.log(err);
}
})
});
//Stream(流) :创建可读可写流
var rStream = fs.createReadStream(newFile,{bufferSize:200});
var wStream = fs.createWriteStream(newFile,{bufferSize:200});
//判断文件是否存在
//同步方法
console.log(fs.existsSync(newFile)); //Result:true
//异步方法
fs.exists(newFile,function (result) {
console.log(result); //Result:true
});
//复制文件,
fs.link(newFile,'./核心库/tpl2.txt',function (err) {
if(err){console.log(err);}
});
//删除文件
fs.unlink('./核心库/tpl2.txt');
//创建文件夹
fs.mkdir('./核心库/mk',function (err) {
if(err){
console.log(err);
}
});
fs.rmdir('./核心库/mk');
2)FileSystem与Path
var fs = require('fs');
var path = require('path');
//递归遍历文件夹下所有文件
function consoleFiles(dir) {
var files = fs.readdirSync(dir); //只能读取下一级的所有文件或文件夹名
files.forEach(function (name) {
var pathName = path.join(dir,name); // 成为一个完整的目录
if(fs.statSync(pathName).isDirectory()){
consoleFiles(pathName); //遇到文件夹使用递归继续往下获取一级,直至获取到文件名
}else{
console.log(pathName); //遇到文件名即可输出
}
});
}
consoleFiles('./核心库');
3)Path
var path = require('path');
var p = 'https://www.dushusir.com/abc/cde.html';
//路径标准化
var p1 = path.normalize(p);
console.log(p1); //Result: https:\www.dushusir.com\abc\cde.html
//组合多个路径
console.log(path.join('http://dushusir.com/','abc','../abc')); //Result:http:\dushusir.com\abc
//将相对路径换成绝对路径
console.log(path.resolve('./')); //Result: 向上一层 D:\Code\前端开发技术\Node.js\NodeTest
console.log(path.resolve('../')); //Result: 向上两层 D:\Code\前端开发技术\Node.js
//输出路径的直接上级
console.log(path.dirname(p)); //Result: https://www.dushusir.com/abc
//输出路径的最后一部分
console.log(path.basename(p)); //Result: cde.html
//输出扩展名,以最后一个点开始
console.log(path.extname(p)); // Result: .html
//常量
console.log(path.sep); //Result: \
console.log(path.delimiter); //Result: ;