Writing Files in Bnlang

Writing files is a common task when working with file systems.
Bnlang provides both synchronous and asynchronous methods to write data into files.
Synchronous methods block program execution until the operation is finished, while asynchronous methods allow the program to continue running in the background.


Example: Synchronous Write

const fs = require("fs");

fs.writeFileSync("example.txt", "Bnlang থেকে হ্যালো!");
কনসোল.লগ("ফাইল সফলভাবে লেখা হয়েছে!");

Example: Asynchronous Write

const fs = require("fs");

fs.writeFile("example.txt", "হ্যালো বিশ্ব!", (ত্রুটি) => {
  যদি (ত্রুটি) {
    কনসোল.ত্রুটি("ফাইল লেখার সমস্যা:", ত্রুটি);
    ফেরত;
  }
  কনসোল.লগ("অ্যাসিঙ্ক্রোনাসভাবে ফাইল লেখা হয়েছে!");
});

Best Practices

  • Use asynchronous writes in production for better performance.
  • Synchronous writes are suitable for small scripts or setup tasks.
  • Always handle errors when writing files.
  • Use proper encoding like utf-8 for text files.
  • Be careful when overwriting files; check before writing if necessary.