nodejs 讀流寫流以及拷貝常用方法

2021-09-29 16:54:51 字數 1425 閱讀 8980

一、讀流的用法

//a用於檔案追加,不存在則建立

//w+ 檔案讀取和寫入,不存在建立,存在就截斷

const fs =

require

('fs'

)let rs = fs.

createreadstream

('./sss.txt',)

//內部發布訂閱模式

//預設是暫停模式

//監聽data會改為流動模式

let arr =

rs.on

('open'

,function

(fd)

)rs.on(

'data'

,function

(data)

)//固定的,內部emit的data

rs.on

('end'

,function()

)rs.on(

'close'

,function()

)

二、寫流的用法

const fs =

require

('fs'

)let ws = fs.

createwritestream

('./jj.txt',)

let index =

0function

write()

//flag是寫入大於highwatermark就會false

if(index===10)

}write()

ws.on

('drain'

,function()

)

三、拷貝

const fs =

require

('fs'

)rs = fs.

createreadstream

('./sss.txt',)

ws = fs.

createwritestream

('./jj.txt',)

rs.pipe

(ws)

const fs =

require

('fs'

)rs = fs.

createreadstream

('./sss.txt',)

ws = fs.

createwritestream

('./jj.txt',)

rs.on

('data'

,function

(chunk)})

ws.on

('drain'

,function()

)

node的流物件學習(讀流和寫流)

依然用上次的數列實現。這次包含乙個讀流物件,乙個寫流物件。全部 用流來實現斐波那契數列 author yyy var stream require stream var util require util 下面是可讀流 function streamchildread n util.inherits...

IO流的讀與寫

streamreader是專門用來讀取文字檔案的類,streamreader可以從底層stream物件建立streamreader物件的例項,而且也能指定編碼規範引數。建立streamreader物件後,它提供了許多用於讀取和瀏覽字元資料的方法 public static void main str...

簡單的檔案流寫讀

region 檔案流寫入 private void writefileinfo stream myfile.open filemode.openorcreate byte bytearr stream.write bytearr,0,bytearr.length 1 stream.flush str...