From 7b28fed518f38c663ec24c68cd022eaa0e1224b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=B8=BF=E5=A5=87?= <10465832+lhqong@user.noreply.gitee.com> Date: Mon, 20 Feb 2023 03:25:32 +0000 Subject: [PATCH] fixed 0361200 from https://gitee.com/lai123/class6nodehomework/pulls/32 66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘鸿奇 <10465832+lhqong@user.noreply.gitee.com> --- liu/open.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ liu/open.txt | 1 + 2 files changed, 50 insertions(+) create mode 100644 liu/open.js create mode 100644 liu/open.txt diff --git a/liu/open.js b/liu/open.js new file mode 100644 index 0000000..e238b13 --- /dev/null +++ b/liu/open.js @@ -0,0 +1,49 @@ +// ## 作业1:使用open write close的形式去创建一个大文件, +// 内容随机写点数据,计算出这个大文件出现最多的字符. +var fs=require('fs'); +fs.open("open.txt",'w',function(err,fd) { + // length || (length=8); + //const buf1 = Buffer.alloc(5000); + const letters='qwertyuiopasdfghjklzxcvbnm'; + let total=''; + total+=letters; + let result='' + let length=1000; + while(length>990){ + length-- + result+=total[Math.floor(Math.random()*total.length)]; + fs.writeSync(fd,result+''); + + } + fs.close(fd) +}) +var fs = require('fs'); + +// 打开一个流: +var rs = fs.createReadStream('open.txt', 'utf-8'); + +rs.on('data', function (chunk) { + console.log(chunk); + let str=chunk; + let k={} + for (let i = 0; i < str.length; i++) { + let chars=str.charAt(i); + if (k[chars]) { + k[chars]++; + }else{ + k[chars]=1; + } + + } + console.log(k); + let max=0; + let letter='' + for(let i in k){ + if(k[i]>max){ + max=k[i]; + letter=i; + + } + } + console.log(`./open.txt中出现最多的文字是${letter},出现了${max}次`); +}); diff --git a/liu/open.txt b/liu/open.txt new file mode 100644 index 0000000..ed79a11 --- /dev/null +++ b/liu/open.txt @@ -0,0 +1 @@ +iiyiyziyzriyzraiyzrapiyzrapxiyzrapxqiyzrapxqliyzrapxqlk \ No newline at end of file -- Gitee