diff --git "a/\346\236\227\347\217\212/\346\225\260\346\215\256\346\265\201/pig.txt" "b/\346\236\227\347\217\212/\346\225\260\346\215\256\346\265\201/pig.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\346\236\227\347\217\212/\346\225\260\346\215\256\346\265\201/readFile.js" "b/\346\236\227\347\217\212/\346\225\260\346\215\256\346\265\201/readFile.js" new file mode 100644 index 0000000000000000000000000000000000000000..a185088f492159842cf746009e8732e2ab89d5fe --- /dev/null +++ "b/\346\236\227\347\217\212/\346\225\260\346\215\256\346\265\201/readFile.js" @@ -0,0 +1,33 @@ +// 使用open write close的形式去创建一个大文件,内容从指定的文本中随机取一些(每次取得长度内容可以不一样),计算出这个大文件出现最多的字符(要使用流的形式). +const { strict } = require("assert"); +const { log } = require("console"); +let fs = require("fs"); +const { stringify } = require("querystring"); + +let fd = fs.openSync("./pig.txt","a"); +let str = "李靖殷是猪YY"; +for(let i = 0; i < 10; i++) { + fs.writeSync(fd,str + " "); +} +fs.closeSync(fd); + +let steam = fs.createReadStream("./pig.txt"); +let l = []; +steam.on("data", (pig) => { + console.log(pig.toString()); + for(var j = 0; j < pig.toString().length; j++) { + let ch = pig.toString()[j] + "="; + l[ch] > 0 ? l[ch]++ : l[ch] = 1; + } +}); + +steam.on("end", () => { + let max = {name: null, num : 0}; + for (const key in l) { + if(l[key] > max.num) { + max.name = key.slice(0, 1); + max.num = l[key]; + } + } + console.log(max); +}) \ No newline at end of file