node檔案流的讀取與寫入

2021-10-06 14:29:02 字數 926 閱讀 6882

1、檔案流寫入

const fs = require("fs");

const ws = fs.createwritestream("test.txt",);

for(let i = 0;i < 10000;i++),(err)=>);

}ws.end();//標記檔案寫入完成

ws.on("open",()=>)

ws.on("ready",()=>)

ws.on("close",()=>);

ws.on("error",()=>);

2、檔案流讀取

const fs = require("fs");

const rs = fs.createreadstream("test.txt",);//建立讀取檔案入口

let str = "";

rs.on("data",(chunk)=>);

rs.on("open",()=>).on("ready",()=>).on("end",()=>).on("error",()=>);

3、讀取某個檔案,寫入到另乙個檔案中

const fs = require("fs");

const rs = fs.createreadstream("test.txt");

const ws = fs.createwritestream("testcopy.txt",);

rs.on("data",(chunk)=>);

rs.on("close",()=>).on("error",()=>);

ws.on("close",()=>);

4、使用pipe可實現第三個案例,pipe的原理類似於第三個案例

rs.pipe(ws);

node讀取流,寫入流和管道流

const fs require fs let readstream fs.createreadstream 要讀取的檔案路徑 let str reasstream.on data data readstream.on close readstream.on error err const fs r...

node 讀取 xlsx檔案寫入檔案

node讀取.xlsx檔案 var fs require fs var events require events 引入node,解析.xlsx檔案的模組 var xlsx require node xlsx var eventemitter newevents.eventemitter var w...

檔案的讀取與寫入

檔案的建立 creat file writename new file d 相對路徑,如果沒有則要建立乙個新的output。txt檔案 writename.createnewfile 建立新檔案 bufferedwriter out new bufferedwriter new filewriter...