zoukankan      html  css  js  c++  java
  • node--初步了解-01 小文件简单读写

    let fs = require('fs');
    // 方法都是 异步没有sync / 同步 Sync
    // 返回值可以获取同步的结果
    let path = require('path');
    // 读取文件默认的结果类型 encoding:null 默认是buffer
    // 如果文件不存在则会报错
    // 读取的时候会把内容整体读取到内存中(读小的文件),大的文件流操作
    let r = fs.readFileSync(path.join(__dirname,'note.md'),{encoding:'utf8',flag:'r'});
    console.log(r);

    // error-first
    fs.readFile(path.join(__dirname,'note.md'),'utf8',function(err,data){ // 回调中的第一个参数 永远是错误
    console.log(data);
    });
    console.log(r);

    根据上面的文档 使用node 读写 小文件  ,这里 node 读取文件 放入到内存中,然后在写,千万别用 大文件·· ,大文件建议使用 流

    /**
     *@读取 写入文件
     */
    let fs =require("fs");
    let path =require("path");
    
    // fs.writeFile(path.join(__dirname,"1.txt"),"今天天气好晴朗··",function (err, data) {
    //     console.log("ok")
    // });
    
    //小文件 简单 拷贝
    fs.readFile(path.join(__dirname,"1.txt"),(err,data)=>{
        fs.writeFile(path.join(__dirname,"2.txt"),data,(err,data)=>{
            console.log("拷贝 ok");
        })
    })
    

      

  • 相关阅读:
    mybatis 入门基础
    spring学习总结
    综合练习:词频统计
    组合数据类型综合练习
    Python基础综合练习
    熟悉常用的Linux操作
    1.大数据概述
    C程序语法(无左递归)
    文法规则
    实验一词法分析报告
  • 原文地址:https://www.cnblogs.com/liujzcom/p/9520992.html
Copyright © 2011-2022 走看看