zoukankan      html  css  js  c++  java
  • Node.js 中的重要API:命令行工具以及FS API 首个Node应用

    2019-12-16

    17:05:54

     

     

     

     

     

     

     

     

    var fs = require('fs');
    fs.readdir(__dirname,function(err,files){
        console.log(files);
    });

     

     

     

     

    /**
     * Module dependencies.
     */
    
    var fs = require('fs');
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      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) {
          if (stat.isDirectory()) {
            console.log('     '+i+'   33[36m' + filename + '/33[39m');
          } else {
            console.log('     '+i+'   33[90m' + filename + '33[39m');
          }
    
          if (++i == files.length) {
            console.log('');
            process.stdout.write('   33[33mEnter your choice: 33[39m');
            process.stdin.resume();
            process.stdin.setEncoding('utf8');
          } else {
            file(i);
          }
        });
      }
    
      file(0);
    
    });

     

     

    /**
     * Module dependencies.
     */
    
    var fs = require('fs')
      , stdin = process.stdin
      , stdout = process.stdout
    
    /**
     * Read the current directory.
     */
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      console.log('   Select which file or directory you want to see
    ');
    
      // called for each file walked in the directory
      function file(i) {
        var filename = files[i];
    
        fs.stat(__dirname + '/' + filename, function (err, 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);
          }
        });
      }
    
      // read user input when files are shown
      function read () {
        console.log('');
        stdout.write('   33[33mEnter your choice: 33[39m');
    
        stdin.resume();
        stdin.setEncoding('utf8');
        stdin.on('data', option);
      }
    
      // called with the option supplied by the user
      function option (data) {
        if (!files[Number(data)]) {
          stdout.write('   33[31mEnter your choice: 33[39m');
        } else {
          stdin.removeListener('data', option);
        }
      }
    
      // start by walking the first file
      file(0);
    });

     

     完成:

    /**
     * Module dependencies.
     */
    
    var fs = require('fs')
      , stdin = process.stdin
      , stdout = process.stdout
    
    /**
     * Read the current directory.
     */
    
    fs.readdir(__dirname, function (err, files) {
      console.log('');
    
      if (!files.length) {
        return console.log('    33[31m No files to show!33[39m
    ');
      }
    
      console.log('   Select which file or directory you want to see
    ');
    
      // called for each file walked in the directory
      function file(i) {
        var filename = files[i];
    
        fs.stat(__dirname + '/' + filename, function (err, 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);
          }
        });
      }
    
      // read user input when files are shown
      function read () {
        console.log('');
        stdout.write('   33[33mEnter your choice: 33[39m');
    
        stdin.resume();
        stdin.setEncoding('utf8');
        stdin.on('data', option);
      }
    
      // called with the option supplied by the user
      function option (data) {
        if (!files[Number(data)]) {
          stdout.write('   33[31mEnter your choice: 33[39m');
        } else {
          stdin.removeListener('data', option);
        }
      }
    
      // start by walking the first file
      file(0);
    });

     

      

     

     

     

      

     

     

  • 相关阅读:
    java 各个文件夹的含义
    对称加密 & 非对称加密
    leetcode 155 最小栈
    leetcode 53 最大自序列和
    leetcode 146 LRU 缓存机制
    notebook 开启 有限元学习
    leetcode 232 用栈实现队列
    LINUX装机问题:无法使用“Ctrl+Alt+[F1~F6]”快捷键切换到终端
    JAVA笔记4-static关键字
    C++构造函数、析构函数、虚析构函数
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12051041.html
Copyright © 2011-2022 走看看