當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


node.js Stream writable.setDefaultEncoding()用法及代碼示例


writable.setDefaultEncoding()方法是Stream模塊的內置應用程序編程接口,用於為Writable流設置默認編碼。

用法:

writable.setDefaultEncoding( encoding ) 

參數:此方法接受單參數編碼,該參數保留要用於可寫流的編碼。


返回值:它返回默認編碼。

以下示例說明了Node.js中writable.setDefaultEncoding()方法的使用:

範例1:

// Node.js program to demonstrate the      
// writable.setDefaultEncoding() method   
const stream = require('stream'); 
  
// Creating a stream and creating  
// a write function 
const writable = new stream.Writable({ 
  
  // Write function with its  
  // parameters 
  write:function(chunk, encoding, next) { 
  
    // Converting the chunk of 
    // data to string 
    console.log(chunk.toString()); 
    next(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Calling setDefaultEncoding method 
writable.setDefaultEncoding("utf8");

輸出:

hi
Writable {
  _writableState:
   WritableState {
     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,
     ending:false,
     ended:false,
     finished:false,
     destroyed:false,
     decodeStrings:true,
     defaultEncoding:'utf8',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:1,
     prefinished:false,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:true,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

此處,默認值在輸出中返回。

範例2:

// Node.js program to demonstrate the      
// writable.setDefaultEncoding() method   
const stream = require('stream'); 
  
// Creating a stream and creating  
// a write function 
const writable = new stream.Writable({ 
  
  // Write function with its  
  // parameters 
  write:function(chunk, encoding, next) { 
  
    // Converting the chunk of 
    // data to string 
    console.log(chunk.toString()); 
    next(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Calling setDefaultEncoding method 
writable.setDefaultEncoding("ascii");

輸出:

hi
Writable {
  _writableState:
   WritableState {
     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,     ending:false,
     ended:false,
     finished:false,
     destroyed:false,
     decodeStrings:true,
     defaultEncoding:'ascii',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:1,
     prefinished:false,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:true,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

此處,默認值為“ascii”。

參考: https://nodejs.org/api/stream.html#stream_writable_setdefaultencoding_encoding



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.setDefaultEncoding() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。