Support Online
Skip to main content

File Management with Node.js Streams

In this guide, you will learn how to work efficiently with files using the Node.js streaming system (Streams).
You'll read, write, copy, and even reverse content without bloating memory.
So in short, we will master “fs.createReadStream”, “pipe()” and “Transform()”. 😎

🚀 What You Will Learn in This Guide

  • Stream concept and its advantages
  • File reading: fs.createReadStream()
  • Writing to file: fs.createWriteStream()
  • File copy: pipe()
  • Data conversion: class Transform()
  • A mini command line based CLI tool

💡 What is Stream and Why is It Used?

Stream allows data to be processed piece by piece rather than all at once.
So you can process even gigabyte files without loading them into memory.

Advantages:

  1. Memory Efficiency: It saves RAM because data is processed on a "chunk" basis.
  2. Time Efficiency: The process starts before the entire data is loaded.
Stream TypeDescription
ReadableThe source from which data is read (fs.createReadStream).
WritableThe destination to which data is written (fs.createWriteStream).
DuplexStream that can both read and write.
TransformBidirectional flow that processes and transforms data.

⚙️ 1. Command Line Program Skeleton

In the first step, let's set up the CLI structure.

mkdir genixnode-streams && cd genixnode-streams
nano cli-araci.js
chmod +x cli-araci.js
#!/usr/bin/env node
const fs = require('fs');
const readline = require('readline');
const stream = require('stream');
const args = process.argv;
const commands = ['oku', 'yaz', 'kopyala', 'terscevir'];

This skeleton defines CLI commands as “read,” “write,” “copy,” and “reverse.”


📖 2. Reading a File — createReadStream()

function oku(filePath) {
const readableStream = fs.createReadStream(filePath, 'utf8');

readableStream.on('data', chunk => console.log(chunk));
readableStream.on('error', err => console.error(`Hata: ${err.message}`));
}

This function reads the file piece by piece and writes each chunk to the terminal.

./cli-araci.js oku veri.txt

✍️ 3. Writing to File — createWriteStream()

function yaz(filePath) {
const writableStream = fs.createWriteStream(filePath);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'Cümle gir (bitirmek için exit): '
});

rl.prompt();
rl.on('line', line => {
if (line.trim() === 'exit') rl.close();
else { writableStream.write(line + '\n'); rl.prompt(); }
}).on('close', () => {
writableStream.end();
console.log(`${filePath} dosyasına yazma tamamlandı.`);
});
}

It instantly saves every line you type from the terminal to a file.


🧩 4. File Copy — pipe()

function kopyala(filePath) {
const copyPath = filePath.replace('.', '-kopya.');
fs.createReadStream(filePath)
.pipe(fs.createWriteStream(copyPath))
.on('finish', () => console.log(`Dosya kopyalandı: ${copyPath}`));
}

The pipe() method automatically connects the read stream to the write stream.


🔄 5. Reverse Content — Transform()

const { Transform } = stream;

function terscevir(filePath) {
const outPath = filePath.replace('.', '-ters.');
const reverseStream = new Transform({
transform(data, enc, cb) {
this.push(data.toString().split('').reverse().join(''));
cb();
}
});

fs.createReadStream(filePath)
.pipe(reverseStream)
.pipe(fs.createWriteStream(outPath))
.on('finish', () => console.log(`Ters çevirme tamamlandı: ${outPath}`));
}

It reverses the read data and writes it to the new file.


❓ Frequently Asked Questions

  1. What is the difference between Stream and fs.readFileSync?

readFileSync puts the entire file into memory. Stream processes the data piece by piece.

  1. Why is pipe() preferred?

Manages flow control for you; It automatically performs error and speed optimization.

  1. Where is Transform() used?

When you want to transform data while processing it — such as compression, encryption, formatting.

  1. How to manage errors in flow?

All streams are EventEmitter. You can catch errors with stream.on('error', ...).


🏁 Conclusion

You now know how to work with files in a stream-based way in Node.js. You can build memory-friendly, high-performance systems and manage data flow professionally.

You can try these techniques immediately on the GenixNode Platform and run Node.js applications with high performance. 🚀