1.Buffer提供了四個(gè)方法來申請內(nèi)存
Buffer.from那伐、
Buffer.alloc品嚣、
Buffer.allocUnsafe均唉、
Buffer.allocUnsafeSlow
2.Buffer靜態(tài)方法與實(shí)例方法
Buffer.byteLength -- 返回一個(gè)字符串的實(shí)際字節(jié)長度
Buffer.isBuffer:判斷參數(shù)中的數(shù)據(jù)是否是Buffer實(shí)例
Buffer.isEncoding; -- 判斷Buffer是否支持該編碼
Buffer.write -- 向Buffer實(shí)例中寫入數(shù)據(jù)
buf.equals -- 判斷兩個(gè)Buffer實(shí)例是否相等
buf.indexOf -- 檢索特定字符串在整個(gè)字符串中的位置
buf.slice -- 截取Buffer實(shí)例的一部分,生成一個(gè)新的Buffer實(shí)例
申請內(nèi)存
const buf1 = Buffer.alloc(5); 《Buffer 00 00 00 00 00》
let buf2 = Buffer.from('abc'); 《Buffer 61 62 63》
let buf3 = Buffer.from([1,2,3]); 《Buffer 01 02 03》
let buf4 = Buffer.from(buf3); 《Buffer 01 02 03》
------------ Buffer.byteLength 統(tǒng)計(jì)特定編碼下字符串對應(yīng)的字節(jié)數(shù) ------------
const one = 'Hello world';
const two = Buffer.byteLength(one, 'utf8');
console.log( two); ----結(jié)果11
------------ Buffer.isBuffer 判斷參數(shù)中的數(shù)據(jù)是否為Buffer實(shí)例 ------------
const buf = Buffer.alloc(5);
console.log(Buffer.isBuffer(buf));----結(jié)果true
------------ Buffer.isEncoding 判斷Buffer是否支持該編碼 ------------
console.log(Buffer.isEncoding('ascii'));---結(jié)果true
------------ Buffer.write 向Buffer實(shí)例中寫入文件 ------------
encoding --- string的字符編碼
buf.write(string[, offset[, length]][, encoding])
參數(shù)一:要寫入buf的字符串
參數(shù)二:開始寫的位置
參數(shù)三:寫入的長度
參數(shù)四:編碼形式
let two = Buffer.alloc(5);
two.write('abcd',1,2,'utf8');
console.log(two);---結(jié)果是《buffer 00 61 62 00 00》
------------ Buffer.equals 判斷兩個(gè)Buffer實(shí)例是否相等 ------------
let one1 = Buffer.from('abc');
let one2 = Buffer.from('abcd');
console.log(one1.equals(one2)); ---結(jié)果是false
------------ Buffer.indexOf 檢索特定字符串在整個(gè)Buffer中的位置 ------------
const buf = Buffer.from('this is a Buffer');
console.log(buf.indexOf('is'));---結(jié)果是2
------------ Buffer.slice 截取Buffer實(shí)例的一部分竟稳,生成一個(gè)新的Buffer實(shí)例 ------------
const buf = Buffer.from('HelloWorld');
console.log(buf.slice(2,4)); ---結(jié)果是《buffer 6c 6c》
// 要引入模塊
const path = require('path');
------------ path.join 拼接路徑 -----------
let str1 = path.join('/foo', 'bar', 'baz/asdf', 'quux');
let str2 = path.join(__dirname,'abc.txt')
console.log(str1);\foo\bar\baz\asdf\quux
console.log(str2);\當(dāng)前目錄\abc.txt
------------ path.basename 得到文件的名稱------------
let one = path.basename('/foo/bar/baz/asdf/quux.html');
console.log(one); // quux.html
let one = path.basename('/foo/bar/baz/asdf/quux.html','.html');
console.log(one); // quux
------------ path.dirname 得到文件的路徑------------
let one = path.dirname('/foo/bar/baz/asdf/quux.html');
console.log(one);///foo/bar/baz/asdf
------------ path.extname 得到文件的拓展名------------
let one = path.extname('/foo/bar/baz/asdf/quux.html');
console.log(one); // .html
------------ path.parse 把字符串形式的路徑轉(zhuǎn)化為對象形式------------
let one = path.parse('/foo/bar/baz/asdf/quux.html');
console.log(one);
----------- path.format 把對象形式的路徑轉(zhuǎn)化為字符串形式-----------
let one = { root: '/',
dir: '/foo/bar/baz/asdf',
base: 'quux.html',
ext: '.html',
name: 'quux' };
let two = path.format(one);
console.log(two);
------------ path.normalize 將路徑進(jìn)行標(biāo)準(zhǔn)化 -----------
let one = "C:\\temp\\\\foo\\bar\\..\\";
let two = "C:\\temp\\\\foo\\bar\\";
console.log(path.normalize(one)); //\temp\foo\ 因?yàn)?.\所以bar去掉了
console.log(path.normalize(two)); //\temp\foo\ bar
------------ path.relative 計(jì)算相對路徑 -----------
相對路徑:是從當(dāng)前路徑開始的路徑
let str1 = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
let str2= path.relative(__dirname,'其他筆記.md')
console.log(str1);// ..\..\impl\bbb
console.log(str2);// 其他筆記.md'
------------ path.resolve 計(jì)算絕對路徑 -----------
絕對路徑:是從盤符開始的路徑,形如
C:\windows\system32\cmd.exe
let str = path.resolve(__dirname,'001_let.js')
console.log(str);//\Users\wwx478512\Desktop\node\001_let.js
// 引入模塊
const path = require('path');
const fs = require('fs');
------------ fs.exists:判斷文件是否存在 ------------
fs.exists(path.join(__dirname, 'abc.txt'),function(exists){
if (exists) {
console.log("文件存在");
}else {
console.log("文件不存在");
}
})
------------ fs.stat; 判斷文件是否存在 ------------
fs.stat(path.join(__dirname,'abc.txt'), (err, stats) => {
if (!err && stats.isFile()) {
console.log('文件存在');
}else{
console.log('文件不存在');
}
})
------------ fs.access; 判斷文件是否存在 ------------
fs.access(path.join(__dirname,'abc.txt'),(err) => {
if (!err) {
console.log('文件存在');
}else {
console.log('文件不存在');
}
})
------------ 查看文件的狀態(tài)信息 ------------
// 異步方式
fs.stat(path.join(__dirname,'abc.txt'),(err, stats)=>{
console.log(err);
console.log(stats);
console.log('atime:訪問時(shí)間 --- ' + stats.atime);
console.log('mtime:內(nèi)容修改時(shí)間 --- ' + stats.mtime);
console.log('ctime:文件狀態(tài)修改時(shí)間 --- ' + stats.ctime);
console.log('btime:創(chuàng)建時(shí)間 --- ' + stats.btime);
})
// 同步方法
let ret = fs.statSync(path.join(__dirname,'abc.txt'));
console.log(ret)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者