• nodejs文本文件的读写


    文本文件的换行符

    方法一:
    var EOL = fileContents.indexOf(" ") >= 0 ? " " : " ";

    方法二:
    var EOL = (process.platform === 'win32' ? ' ' : ' ')

    删除文件

    var fs = require("fs");
    
    console.log("Going to delete an existing file");
    fs.unlink('input.txt', function(err) {
       if (err) {
          return console.error(err);
       }
       console.log("File deleted successfully!");
    });
    

    小文件的异步与同步读写

    var fs = require("fs");
    
    console.log("Going to write into existing file");
    fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
       if (err) {
          return console.error(err);
       }
       
       console.log("Data written successfully!");
       console.log("Let's read newly written data");
       
       fs.readFile('input.txt', function (err, data) {
          if (err) {
             return console.error(err);
          }
          console.log("Asynchronous read: " + data.toString());
       });
    });
    
    同步和异步的对比:
    var fs = require("fs");
    
    // Asynchronous read
    fs.readFile('input.txt', function (err, data) {
       if (err) {
          return console.error(err);
       }
       console.log("Asynchronous read: " + data.toString());
    });
    
    // Synchronous read
    var data = fs.readFileSync('input.txt');
    console.log("Synchronous read: " + data.toString());
    
    console.log("Program Ended");
    

    读写大文件

    var fs = require("fs");
    var data = '';
    
    // Create a readable stream
    var readerStream = fs.createReadStream('input.txt');
    
    // Set the encoding to be utf8. 
    readerStream.setEncoding('UTF8');
    
    // Handle stream events --> data, end, and error
    readerStream.on('data', function(chunk) {
       data += chunk;
    });
    
    readerStream.on('end',function() {
       console.log(data);
    });
    
    readerStream.on('error', function(err) {
       console.log(err.stack);
    });
    
    console.log("Program Ended");
    输出如下:
    Program Ended
    Tutorials Point is giving self learning content
    to teach the world in simple and easy way!!!!!
    

    使用async/await

    const fs = require('fs').promises;
    
    try {
      const data = await fs.readFile('file1.js'); // need to be in an async function
      console.log(data); // the contents of file1.js
    } catch (error) {
      console.log(error)
    }
    
    const data = "Hello my name is Hugo, I'm using the new fs promises API";
    
    try {
      await fs.writeFile('file1.txt', data); // need to be in an async function
    } catch (error) {
      console.log(error)
    }
    
  • 相关阅读:
    链表的一些规律总结
    acclib的尝试
    初入指针
    在一个堆成矩阵中的循环判断(井字棋游戏)
    初学c语言的小套路汇总
    在循环中计算式一正一负的处理
    最大公约数的计算方法
    大数加法
    大数乘法
    复制可见区域到新表
  • 原文地址:https://www.cnblogs.com/itech/p/13209962.html
Copyright © 2020-2023  润新知