• 【Node】fs


    var fs = require('fs') // fs 文件系统
    var stdin = process.stdin
    var stdout = process.stdout
    var stats = []
    
    fs.readdir(process.cwd(), function(err, files) {
        console.log('')
        if (!files.length) {
            return console.log('    03[31m No files to show!03[39
    ')
        }
        console.log('    Select which file or directory you want to see
    ')
    
        function file(i) {
            var filename = files[i]
            fs.stat(__dirname + '/' + filename, function(err, stat) { // fs.stat会给出文件或目录的元数据
                stats[i] = stat
                if (stat.isDirectory()) {
                    console.log('    '+i+'    33[36m' + filename + '/33[39m')
                } else {
                    console.log('    '+i+'    33[90m' + filename + '/33[39m')
                }
                if (++i == files.length) {
                    read()
                } else {
                    file(i)
                }
            })
        }
        function read() {
            console.log('')
            stdout.write('    33[33mEnter you choice: 33[39m')
            stdin.resume()
            stdin.setEncoding('utf-8')
            stdin.on('data', option)
        }
    
        function option(data) {
            var filename = files[Number(data)]
            if (!filename) {
                stdout.write('   33[31mEnter your choice: 33[39m')
            } else {
                stdin.pause()
                if (stats[Number(data)].isDirectory()) {
                    fs.readdir(__dirname + '/' + filename, function(err, files) {
                        console.log('')
                        console.log('    (' + files.length + ' files)')
                        files.forEach(function(file) {
                            console.log('    -    ' + file)
                        })
                        console.log('')
                    })
                } else {
                    fs.readFile(__dirname + '/' + filename, 'utf-8', function(err, data) {
                        console.log('')
                        console.log('33[90m' + data.replace(/(.*)/g, '    $1') + '33[39m')
                    })
                }
            }
        }
    
        file(0)
    })

    console.log('Hello world')
    process.stdout.write('Hello world')
    console.log('Hello world')

    console.log内部做了这样的事情:它在指定的字符串后加上 (换行)字符,并将其写到stdout流中

  • 相关阅读:
    R语言 dbWriteTable 写入数据库 为空和乱码问题
    data.table进阶
    简述ODS,和数据仓库做简单的比较
    深入ff and ffbase
    R语言操作mysql上亿数据量(ff包ffbase包和ETLUtils包)
    基于mondrain 的原理纠正特殊指标值
    基于mondrian聚合表的R计算olap开发
    dplyr快速入门
    R中的data.table 快速上手入门
    删除pentaho用户和用户文件夹
  • 原文地址:https://www.cnblogs.com/jzm17173/p/3438609.html
Copyright © 2020-2023  润新知