1.1.1使用readline模块逐行读取流数据。创建Interface对象在readline模块中,使用Interface对象实现数据的逐行读取流处理。因此,必须首先创建接口对象。在readline模块中,可以通过createInterface方法创建Interface对象。readline.createInterface(options),options是一个对象,属性如下input:属性值是一个对象,可以用来读取流数据。用于指定读取数据的来源。output:属性值是一个可用于写入流数据的对象,用于指定数据的输出目标。computer:属性值是一个函数,用于指定Tab补全的处理。该函数的参数值自动设置为从该行读取的Tab字符之前的数据,该函数应返回一个数组,该数组由所有用于Tab补全的匹配字符串和从该行读取的Tab字符之前的数据组成。terminal:这个属性是一个布尔属性。当需要像终端一样实时输出输入数据流,并且需要在输出数据中写入ANSI/VT100控制字符串时,需要将该属性的值设置为true,默认属性值为等于输出属性值对象的isTTY属性值。//当输入exit,quit,q中的任意一个时,都会退出});rl.on('line',(line)=>{if(line==='exit'||line==='quit'||line==='q'){rl.close();}else{console.log('Youentered:',line);}});rl.on('close',()=>{console.log('line数据读取操作终止');});函数完成器(行){constcompletions='.help.error.exit.quit.q'.split('');让hits=completions.filter((c)=>{returnc.indexOf(line)===0;});返回[hits.length?点击:完成,行]}1.2。使用Interface对象逐行读取原始fs.js文件的内容console.log('thisisline1');console.log('thisisline2');console.log('thisisline3');console.log('这是第4行');console.log('这是第5行');代码内容constreadline=require('readline');constfs=require('fs');letfile=fs.createReadStream('./fs.js');letout=fs.createWriteStream('./anotherFs.js');letindex=1;out.write('/*line'+index.toString()+":*/");letrl=readline.createInterface({输入:文件,输出:out,terminal:true});rl.on('line',(line)=>{if(line===''){rl.close();}else{index++;out.write('/*line'+index.toString()+':*/');}});生成的anotherFs.js文件的内容/*line1:*/console.log('thisisline1');/*line2:*/console.log('thisisline2');/*line3:*/console.log('这是第3行');/*line4:*/console.log('这是第4行');/*line5:*/console.log('这是第5行');/*第6行:*/2。使用util模块中提供的一些方法+format方法类似于C语言中的printf方法,第一个参数值作为格式化字符串,其他参数值作为使用的参数格式字符串,并返回格式化的字符串。util.format('你输入了%d个参数,参数值为%s,%s,%s',3,'nice','excelent','holy');在格式字符串中,可以使用参数说明符号*`%s`:用于指定字符串参数*`%d`:用于指定数值参数,包括整数和浮点数*`%j`:用于指定`JSON`对象*`%%`:用于指定百分号*如果格式字符串中使用的参数个数多于格式化方法中除`format`参数以外的参数个数,则参数越多格式字符串不会被替换。`console.log(util.format('%s:%s','one'));`*如果格式字符串中使用的参数个数小于`format`方法中使用的`format`参数以外的参数,会根据大于参数值的`format`方法的类型自动转换为字符串,并用空格隔开。+inspect(object,[options])返回一个字符串,其中包含对象的信息,这在调试应用程序的过程中非常有用。*`showHidden
