Node.Js Stream(流)-(三)

来源:互联网 发布:hdmi网络传输器设置 编辑:程序博客网 时间:2024/06/05 06:20

Stream流之fs模块读取写入文件实例二

一、分段读取写入文件

//读取流、写入流实现赋值var fs=require('fs');var path=require('path');var file1=path.resolve('/test1/one.txt');var file2=path.resolve('/test1/one_copy2.txt');//创建读取流var readable=fs.createReadStream(file1,{highWaterMark:6});//创建写入流var writeable=fs.createWriteStream(file2);readable.on('data',function(chunk){console.log('当前读取内容:'+chunk.toString());//从读取流读取内容,添加到写入流writeable.write(chunk);});readable.on('end',()=>{console.log('读取文件结束');//标记写入文件结束writeable.end();});writeable.on('finish',()=>{console.log('--》写入文件结束');});

二、管道流方式读取写入文件

//管道流实例var fs=require('fs');var path=require('path');var file1=path.resolve('/test1/one.txt');var file2=path.resolve('/test1/one_copy.txt');var readable=fs.createReadStream(file1,{highWaterMark:6});var writeable=fs.createWriteStream(file2);//管道流写入readable.pipe(writeable);readable.on('open',function(){console.log('文件打开成功');});readable.on('end',function(){console.log('读取文件结束');});console.log('程序执行完毕');

Steam流实例二

Stream流简介一

0 0