Reading Files in Bnlang
Reading files is a core operation in most applications.
Bnlang provides both synchronous and asynchronous methods for reading files.
Synchronous methods block the program until the file is read, while asynchronous methods allow the program to continue running while the file is being processed.
Example: Synchronous Read
const fs = require("fs");
const তথ্য = fs.readFileSync("example.txt", "utf-8");
কনসোল.লগ("ফাইলের কন্টেন্ট:", তথ্য);
Example: Asynchronous Read
const fs = require("fs");
fs.readFile("example.txt", "utf-8", (ত্রুটি, তথ্য) => {
যদি (ত্রুটি) {
কনসোল.ত্রুটি("ফাইল পড়ার সমস্যা:", ত্রুটি);
ফেরত;
}
কনসোল.লগ("ফাইলের কন্টেন্ট:", তথ্য);
});
Best Practices
- Prefer asynchronous methods in production for scalability.
- Use synchronous methods only for small scripts or quick utilities.
- Always handle errors properly when reading files.
- Use
utf-8
encoding for text files unless binary data is required.