Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use stream to achieve read-write synchronization while reading and writing in Node.js

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Node.js how to use stream to achieve read-write synchronization while reading and writing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Node.js uses streaming to achieve read-write synchronization while reading and writing".

The specific code is as follows:

/ / 10 bytes, read 4b each time, and write 1blet fs=require ("fs") Function pipe (source,target) {/ / first create a readable stream, then create a writable stream / / read it first. Rs.on (data) / / writes the read class capacity to the target and returns a Boolean value. If it is ture, continue writing, the default should be false, pause reading / / ws.on ('drain'), drain, reply to read / / listen after reading the file Turn off reading rs.on ('end') let rs=fs.createReadStream (source, {highWaterMark:4}) Let ws=fs.createWriteStream (target, {highWaterMark:1}); rs.on ('data',function (chunk) {/ / chunk is the buffer type if (ws.write (chunk) = false) {/ / cannot write, stop reading rs.pause ()}); ws.on (' drain',function () {/ / resume reading console.log every time the memory is dry) Rs.resume () / / resume reading}); rs.on ('end',function (chunk) {ws.end ();})} pipe ('. / 1.txtread.

The above method of writing is rather tedious. After you have read it, just forget it.

Node.js provides us with the built-in method pipe

/ / 10 bytes, read 4b each time, write 1blet fs=require ("fs"); function pipe (source,target) {let rs=fs.createReadStream (source, {highWaterMark:4}); let ws=fs.createWriteStream (target, {highWaterMark:1}); / / Readable stream to writable stream, asynchronous operation, can ensure that memory will not be flooded, read a little, write a little / / if you want to see the file content, use readFile rs.pipe (ws) } pipe ('. / 1. Txthammer. Universe 4.txt')

Directly operate the read stream into the write stream, or asynchronously

At this point, I believe you have a deeper understanding of "how to use streaming to achieve read-write synchronization while reading and writing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report