node.js basic file operations and notes

  • Modular:

    In node in a js is a module.

    ) Referenced by external modules require (. If the path is relative, or ./ .. need to begin.

    A file is a module, there is an imaginary function wrapped, so I want to call on the outside, you need to use export.x

  • The module may include references

    • The core module directly write the name on the list.

    For example, it fs module

    • File module
  • js in the window here is global

  • Recommended module.exports.xxx way, we can declare external calls.

  • Some operations of npm

    • npm -v view the version of npm
    • npm version View all versions of the modules
    • npm search package name search name
    • npm install package name installation (i may be used instead of install)
    • npm install package name --save installation package dependencies and added to
    • npm remove the package name to delete package (can substitute r remove)
    • npm install download the current project depends package
    • npm install -g global installed package package name (usually the tool)
  • Node basis

    • The concept of buffer zone buffer

      var buf = Buffer.from(str);
      var buf2 = Buffer.alloc(10);
      
      • After determining the size of the buffer is not modified.

      • The actual hexadecimal memory, but so will the real output to the console into a decimal.

      • Buffer.from (str) into a string buffer

      • Buffer.toString () of the data buffer into a string

    • File system

      • Files by node to operate the system.

      • var fs = require("fs");
        
      • Usually two groups, with a sync synchronization, without asynchronous.

      • Write sync file

      • var fd = fs.openSync("xxx.txt", "w");
        fs.writeSync(fd, "今天天气不错");
        fs.closeSync(fd);
        
      • Asynchronous file write

        • Relatively good performance
        • The results returned by the callback function
        • The callback function takes two parameters:
          • err error object
          • file descriptor fd
        • Asynchronous later are performed after the code in the callback function
      • //多一个回调函数 ,异步方法没有返回值
        fs.open("xxx2.txt", "w",function(err, fd){
            if(!err){
        			fs.write(fd, "今天天气不错" ,function(err){
                if(!err){
                  .写入成功.
                }
              });
        			fs.closeSync(fd,function(err){
                if(!err){
                   .文件成功关闭.
                }
              });
            }else {
              console.log(err);
            }
        });
        
      • Simple file write

        • var fs = require("fs");
          fs.writeFile("hello3.txt", "这是通过writeFile写入的内容",{flag:"w"},function(err){
            if(!err){
              console.log("写入成功...");
            }
          });
          
      • Streaming files are written

        • Written for large files

        • on (Event String, callback)

        • once (Event String, callback) to bind a one-off

        • var ws = fs.createWriteStream("Hello3.txt");
          ws.once("open", function(){
            console.log("233");
          })
          ws.once("close", function(){
            console.log("233");
          })
          ws.write("通过可写流写入文件的内容");
          ws.write("通过可写流写入文件的内容");
          ws.write("通过可写流写入文件的内容");
          
          ws.end();
          //不要用ws.close(),拔掉水管的那一头
          
      • Synchronize files to read

      • Asynchronous file read

      • Simple file read

        var fs = require("fs");
        fs.readFile("hello3.txt", function(err, data){
          if(!err) {
            console.log(data);
        		//也可以整合上面的writeFile
          }
        })
        
      • Streaming file read

        • It can be divided into multiple times to read a large file into memory.

        • var fs = require("fs");
          var rs = fs.createReadStream("an.jpg");
          //监听流的开启和关闭
          rs.once("open", function(){
            console.log("可读流打开了~~~");
          });
          rs.once("close", function(){
            console.log("可读流关闭了~~~");
          });
          
          //为data绑定事件,就可以自动运行
          rs.on("data", function(){
            //实现复制的效果。
            ws.write(date);
          });
          
          //传输数据,还可以用pipe简化
          rs.pipe(ws);
          
      • Some other methods of file operations

        • fs.stat("a.mp3", function(err, stat){
          	//查看是不是文件、目录
            console.log(stat.isFile());
            console.log(stat.isDirectory());
            
          });
          //删除文件
          fs.unlinkSync("hello.txt");
          //读取当前文件目录结构
          fs.readdir(".", function(err, files){
            if(!err){
              console.log(files);
            }
          });
          
          //截断文件,将文件前3个留下来。
          fs.truncateSync("hello2.txt", 3);
          //创建文件夹
          fs.mkdirSync("hello");
          //删除文件夹
          fs.rmdirSync("hello");
          //重命名,前两个是目录字符串
          fs.rename("a.mp3", "笔记.mp3",function(err) {
            if(!err){
              console.log("修改成功~~~");
            }
          });
          
          //监视文件的修改,回调函数,当文件发生变化的,被执行。
          //运行起来,就会一直运行,不会停。
          //拥有定时机制
          fs.watchFile("hello2.txt",{interval:1000},function(curr, prev){
            //上面两个参数,都是stat类型的。
            console.log("文件发生了变化");
          });
          
          

Guess you like

Origin www.cnblogs.com/ckxkexing/p/12637198.html