Requirements: Build a command-line file browser that can find files, display files, and create files in the current directory.

Node.js inputs and outputs

The Process object contains three flow objects that correspond to the three UNIX standard streams: STdin, STdout, and stderr

Stdin is a readable stream whose default state is paused. While the stream is running, Node watches the corresponding file descriptor and keeps the event loop running, waiting for the event to fire. If no I/O wait is involved, Node automatically exits.

Stdout and stderr are writable streams. Console. log, which adds a \n newline character to the specified string and writes it to the stdout stream.

 var stdin=require('process').stdin
, stdout=require('process').stdout;
Copy the code
 stdin.resume()
 / / stdin
 stdin.setEncoding('utf8')
 //stdin sets the encoding mode of the input stream
 stdin.on()
 // Listen on the input stream
 stdin.pause()
 // Pause the input stream
Copy the code
stdout.write()
Copy the code

Stream

const fs=require('fs'); const path=require('path'); let readStream=fs.createReadStream('./test/b.js',{encoding:'utf8'}); //console.log(readStream); Readstream. on('error', (err) => {console.log(' error :', err); }); Readstream. on('open', (fd) => {console.log(' file open :', fd); }); Readstream.on ('ready', () => {console.log(' File ready.. '); }); / / file read event... readStream) on (' data '(the chunk) = > {the console. The log (' read file data:' the chunk); }); Readstream. on('end', () => {console.log(' Reading completed.. '); }); Readstream. on('close', () => {console.log(' File closed! ');Copy the code

The fs, speaking, reading and writing

fs.stat(path,callback)

Read the state of the file; You can determine if it’s a file, when the file was last modified, etc

callback: function(err,stats){}

Methods that return stats include isDictionary, etc

fs.readdir(path,callback)

path:

Local directory: process.cwd;

Directory to find: __dirname+’/’+filename

The callback function (err, files)

fs.readfile

Directory to find: __dirname+’/’+filename

The callback function (err, files)

fs.watchfile

var fs=require('fs')
var files=fs.readdirSync(process.cwd())
files.forEach((file) = >{
    if(/\.css/.test(file)){
        fs.watchFile(process.cwd()+'/'+file,function(data){
            console.log(The '-'+file+'changed')})}})Copy the code

The overall implementation

var fs=require('fs')
, stdin=require('process').stdin
, stdout=require('process').stdout;
const { exit } = require('process');
fs.readdir(process.cwd(),function(err,files){
    console.log(' ');
    if(! files.length){return console.log('\033[33m No files to show!\033[39m\n')}console.log('Select which file or directory you want to see :')
    var stats=[]
    function file(i){
        var filename=files[i]
        fs.stat(__dirname+'/'+filename,function(err,stat){
            stats[i]=stat
            if(stat.isDirectory()){
                console.log(' '+i+""+'\033[36m'+filename+'\033[39m')}else{
                console.log(' '+i+""+'\033[90m'+filename+'\033[39m')}if(++i===files.length){
                read()
            }else file(i)
        })
    }
    file(0)
    function read(){
        console.log("")
        stdout.write(' Enter your choice: ')
        stdin.resume()
        stdin.setEncoding('utf8')
        stdin.on('data',option)
    }
    function option(data){
        var filename=files[Number(data)]
        if(stats[Number(data)].isDirectory()){
            fs.readdir(__dirname+'/'+filename,function(err,files){
                console.log(' ')
                console.log('('+files.length+') ')
                files.forEach(function(fileItem,index){
                    console.log(The '-'+fileItem)
                    console.log("")
                    stdin.pause()
                    exit()
                })
            })
        }
        if(! filename){ stdout.write(' \033[31mEnter your choice: \033[39m')}else{
            stdin.pause();
            fs.readFile(__dirname+'/'+filename,'utf8'.function(err,data){
                console.log('\033[90m'+data+'\033[39m')})}})Copy the code