let fs = require ('fs');let stdin = process.stdin, stdout = process.stdout;fs.readdir(process.cwd(),function (err,files) { console.log(files); console.log(''); if (!files.length){ return console.log('NO FILES\n'); }else function file(i) { var filename = files[i]; fs.stat(__dirname + '/' + filename, function (err, stat) { if (stat.isDirectory()) { console.log(i +':Dir:' + filename + '\n'); } else { console.log(i +':File:' + filename + '\n'); } }); i++; console.log("!!!"+i + files.length); if (i === files.length){ read(); //列表扫描完后才进入 }else { file(i); //递归加载 } } file(0);//初始加载})function read() { stdout.write('ENTER THE CHOOSE\n'); stdin.resume();}按照了不起的nodejs 书中的代码敲的。。。。功能:列出当前目录的文件,在最后输出“enter the choose”debug如下!!!15 //第一个是i,第二个是length!!!25!!!35!!!45!!!55ENTER THE CHOOSE1:Dir:.idea2:File:index.js3:File:package.json4:Dir:test5:File:test.txtenter the choose 本应该在最后输出的才对。。ps且如果没有封装read函数 输出的列表顺序是无序的..
添加回答
举报
0/150
提交
取消