P1 緩存 Buffer
1. 創建緩存
var buf = new Buffer(10);
var buf = new Buffer([10, 20, 30, 40, 50]);
//支持"ascii", "utf8", "utf16le", "ucs2", "base64" or "hex"
var buf = new Buffer("Simply Easy Learning", "utf-8");
2. 寫緩存 buf.write(string[, offset][, length][, encoding])
- string:待寫入緩存的數據
- offset:寫入緩存的偏移量
- length:寫入數據的數量
- encoding:編碼模式,默認為utf8
buf = new Buffer(256);
len = buf.write("Simply Easy Learning");
console.log("Octets written : "+ len);
When the above program is executed, it produces the following result ?
Octets written : 20
3. 讀緩存 buf.toString([encoding][, start][, end])
- encoding:編碼模式,默認為utf8
- start:開始讀取的位置
- end:結束讀取的位置
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf[i] = i + 97;
}
console.log( buf.toString('ascii')); // outputs: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5)); // outputs: abcde
console.log( buf.toString('utf8',0,5)); // outputs: abcde
console.log( buf.toString(undefined,0,5)); // encoding defaults to 'utf8', outputs abcde
//運行輸出
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde
4. 轉換成JSON buf.toJSON()
- encoding:編碼模式,默認為utf8
- start:開始讀取的位置
- end:結束讀取的位置
var buf = new Buffer('Simply Easy Learning');
var json = buf.toJSON(buf);
console.log(json);
//運行輸出
[ 83, 105, 109, 112, 108, 121, 32, 69, 97, 115, 121, 32, 76, 101, 97, 114, 110, 105, 110,
103 ]
5. 合并緩存 Buffer.concat(list[, totalLength])
- list:緩存的數組
- totalLength:合并緩沖的總長度
var buffer1 = new Buffer('TutorialsPoint ');
var buffer2 = new Buffer('Simply Easy Learning');
var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 content: " + buffer3.toString());
//運行輸出
buffer3 content: TutorialsPoint Simply Easy Learning
6. 比較緩存 buf.compare(otherBuffer);
- otherBuffer:待比較的緩存
var buffer1 = new Buffer('ABC');
var buffer2 = new Buffer('ABCD');
var result = buffer1.compare(buffer2);
if(result < 0) {
console.log(buffer1 +" comes before " + buffer2);
}else if(result == 0){
console.log(buffer1 +" is same as " + buffer2);
}else {
console.log(buffer1 +" comes after " + buffer2);
}
//運行輸出
ABC comes before ABCD
7. 拷貝緩存 buf.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
- targetBuffer:目標緩存
- targetStart:目標的起始位置
- sourceStart: 源的起始位置
- sourceEnd:源的結束位置
var buffer1 = new Buffer('ABC');
//copy a buffer
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log("buffer2 content: " + buffer2.toString());
//運行輸出
buffer2 content: ABC
8. 分割緩存 buf.slice([start][, end])
- start:開始位置
- end:結束位置
var buffer1 = new Buffer('TutorialsPoint');
//slicing a buffer
var buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: " + buffer2.toString());));
//運行輸出
buffer2 content: Tutorials
9. 緩存長度 buf.length;
- start:開始位置
- end:結束位置
var buffer = new Buffer('TutorialsPoint');
//length of the buffer
console.log("buffer length: " + buffer.length);
//運行輸出
buffer length: 14
P2 文件系統 FileSystem
1. 同步讀取 readFileSync
//沒有聲明encoding時返回二進制數據
var fs = require('fs');
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
//聲明encoding時返回字符串
var fs = require('fs');
var data = fs.readFileSync('input.txt', { encoding: 'utf-8' });
console.log("Synchronous read: " + data.toString());
//使用try..catch處理異常
try{
var err = fs.readFileSync('noneExist.txt');
}catch(err){
console.log(err.message); // 輸出no such file or directory 'noneExist.txt'
}
2. 異步讀取 readFile
//沒有聲明encoding時返回二進制數據
fs.readFile('input.txt', function(err, data){
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
//聲明encoding時返回字符串
fs.readFile('input.txt', {encoding: 'utf-8'}, function(err, data){
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
3. 打開文件 fs.open(path, flags[, mode], callback)
Flag | Description |
---|---|
r | Open file for reading. An exception occurs if the file does not exist. |
r+ | Open file for reading and writing. An exception occurs if the file does not exist. |
rs | Open file for reading in synchronous mode. |
rs+ | Open file for reading and writing, asking the OS to open it synchronously. See notes for 'rs' about using this with caution. |
w | Open file for writing. The file is created (if it does not exist) or truncated (if it exists). |
wx | Like 'w' but fails if the path exists. |
w+ | Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). |
wx+ | Like 'w+' but fails if path exists. |
a | Open file for appending. The file is created if it does not exist. |
ax | Like 'a' but fails if the path exists. |
a+ | Open file for reading and appending. The file is created if it does not exist. |
ax+ | Like 'a+' but fails if the the path exists. |
// 異步打開文件
var fs = require("fs");
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
});
4. 取文件信息 fs.stat(path, callback)
Method | Description |
---|---|
stats.isFile() | Returns true if file type of a simple file. |
stats.isDirectory() | Returns true if file type of a directory. |
stats.isBlockDevice() | Returns true if file type of a block device. |
stats.isCharacterDevice() | Returns true if file type of a character device. |
stats.isSymbolicLink() | Returns true if file type of a symbolic link. |
stats.isFIFO() | Returns true if file type of a FIFO. |
stats.isSocket() | Returns true if file type of asocket. |
var fs = require("fs");
console.log("Going to get file info!");
fs.stat('input.txt', function (err, stats) {
if (err) {
return console.error(err);
}
console.log(stats);
console.log("Got file info successfully!");
// Check file type
console.log("isFile ? " + stats.isFile());
console.log("isDirectory ? " + stats.isDirectory());
});
運行程序后輸出
Going to get file info!
{
dev: 1792,
mode: 33188,
nlink: 1,
uid: 48,
gid: 48,
rdev: 0,
blksize: 4096,
ino: 4318127,
size: 97,
blocks: 8,
atime: Sun Mar 22 2015 13:40:00 GMT-0500 (CDT),
mtime: Sun Mar 22 2015 13:40:57 GMT-0500 (CDT),
ctime: Sun Mar 22 2015 13:40:57 GMT-0500 (CDT)
}
Got file info successfully!
isFile ? true
isDirectory ? false
5. 寫文件 fs.writeFile(filename, data[, options], callback)
- path:文件名稱(包括路徑)
- data:字符串(String)或者數據緩存(Buffer)
- options:可選項,可以設置
encoding
,mode
,flag
; 默認encoding是utf8
, mode是八進制0666
,flag是w
- callback:包括一個錯誤返回參數的回調函數
var fs = require("fs");
console.log("Going to write into existing file");
fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Read newly written data");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
});
//運行后輸出
Going to write into existing file
Data written successfully!
Read newly written data
Asynchronous read: Simply Easy Learning!
6. 讀文件 fs.read(fd, buffer, offset, length, position, callback)
- fd:讀取成功后返回的文件句柄
- buffer:讀取后的數據會寫入到該緩沖區
- offset:寫入緩沖區的偏移地址
- length:讀取的數據數量
- position:讀取的位置,如果為
null
,則從當前位置讀取 - callback:回調函數
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Going to read the file");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
if (err){
console.log(err);
}
console.log(bytes + " bytes read");
// Print only read bytes to avoid junk.
if(bytes > 0){
console.log(buf.slice(0, bytes).toString());
}
});
});
//運行后輸出
Going to open an existing file
File opened successfully!
Going to read the file
97 bytes read
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
7. 關閉文件 fs.close(fd, callback)
- fd:讀取成功后返回的文件句柄
- callback:回調函數
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Going to read the file");
fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
if (err){
console.log(err);
}
// Print only read bytes to avoid junk.
if(bytes > 0){
console.log(buf.slice(0, bytes).toString());
}
// Close the opened file.
fs.close(fd, function(err){
if (err){
console.log(err);
}
console.log("File closed successfully.");
});
});
});
//運行后輸出
Going to open an existing file
File opened successfully!
Going to read the file
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
File closed successfully.
8. 刪除文件 fs.unlink(path, callback)
- path:文件名稱(包括路徑)
- callback:回調函數
var fs = require("fs");
console.log("Going to delete an existing file");
fs.unlink('input.txt', function(err) {
if (err) {
return console.error(err);
}
console.log("File deleted successfully!");
});
9. 建立目錄 fs.mkdir(path[, mode], callback)
- path:文件名稱(包括路徑)
- mode:目錄權限,默認值
0777
- callback:回調函數
var fs = require("fs");
console.log("Going to create directory /tmp/test");
fs.mkdir('/tmp/test',function(err){
if (err) {
return console.error(err);
}
console.log("Directory created successfully!");
});
10. 讀取目錄 fs.readdir(path, callback)
- path:文件名稱(包括路徑)
- callback:回調函數
var fs = require("fs");
console.log("Going to read directory /tmp");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
11. 刪除目錄 fs.rmdir(path, callback)
- path:文件名稱(包括路徑)
- callback:回調函數
var fs = require("fs");
console.log("Going to delete directory /tmp/test");
fs.rmdir("/tmp/test",function(err){
if (err) {
return console.error(err);
}
console.log("Going to read directory /tmp");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
});
P3 數據流 Streams
流是 unix 管道,可以從數據源讀取數據,然后流向另一個目的地。nodejs有4種數據流:
- Readable:可讀流
- Writable:可寫流 ? Stream which is used for write operation.
- Duplex:讀寫流
- Transform:轉換流,輸出數據根據輸入數據計算
1. 讀取流
假設有文本 input.txt
,內容如下:
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
編寫main.js如下:
var fs = require("fs");
var data = '';
//readableStream.setEncoding('utf8'); 可以設置編碼,回調函數中的 chunk 就會是字符串
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
data += chunk;
});
readerStream.on('end',function(){
console.log(data);
});
readerStream.on('error', function(err){
console.log(err.stack);
});
console.log("Program Ended");
//運行輸出
Program Ended
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
2. 改寫流
var fs = require("fs");
var data = 'Simply Easy Learning';
// 創建可寫流
var writerStream = fs.createWriteStream('output.txt');
// 以utf8的編碼寫入數據
writerStream.write(data,'UTF8');
// 標記文件結束
// 當 end() 被調用時,所有數據會被寫入,然后流會觸發一個 finish 事件。
// 調用 end() 之后就不能再往可寫流中寫入數據
writerStream.end();
// 處理結束事件和錯誤事件
writerStream.on('finish', function() {
console.log("Write completed.");
});
writerStream.on('error', function(err){
console.log(err.stack);
});
console.log("Program Ended"); and easy way!!!!!
運行結束后output.txt
的內容為 Simply Easy Learning
3. 管道 Piping
管道是一個很棒的機制,你不需要自己管理流的狀態就可以從數據源中讀取數據,然后寫入到目的地中。
將input.txt的數據寫入到output.txt
var fs = require("fs");
var readerStream = fs.createReadStream('input.txt');
var writerStream = fs.createWriteStream('output.txt');
readerStream.pipe(writerStream);
console.log("Program Ended");
4. 管道 Chaining
鏈接可以將輸出流作為下一個函數的輸入
將壓縮文件input.txt.gz解壓后的內容放到新文件output.txt
var fs = require('fs');
var zlib = require('zlib');
fs.createReadStream('input.txt.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('output.txt'));
console.log("File Compressed.");