加载文本文件并查找文件中的字符数-JavaScript
假设我们有一个data.txt文件,该文件与NodeJS文件位于同一目录中。假设该文件的内容是-
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.
我们需要编写一个JavaScript函数,将此外部文本文件加载到我们的js文件中并返回此文件中的字符数。
示例
让我们为该函数编写代码-
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');
})输出结果
控制台中的输出:-
399
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短