Giả sử file data.txt cùng thư mục với file Node.js, nội dung:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s.
Viết hàm đọc file và trả về số ký tự.
Ví dụ
Mã thực hiện:
const fs = require('fs');
const requireFile = async () => {
const data = fs.readFileSync('./data.txt', 'utf-8');
const len = data.length;
return len;
};
requireFile().then(res => console.log(res)).catch(err => {
console.log('some error occured');
})
Kết quả
Kết quả in ra console:
399