logo

Nodejs Part2

 
 

fs模块: 读取 + 写入文件

fs模块是Node.js官方提供的, 用来操作文件的模块
写入/读取成功err是null, 不成功是obj
// 使用fs方法前, 先要导入fs const fs = require('fs');

fs读取文件

fs.readFile(path options callback) 读取
  1. path: 路径
  1. options: 编码格式(可选)
  1. callback: 回调函数
const fs = require("fs"); fs.readFile("./files/1.txt", "utf8", function (err, data) { if (err) { return console.log("err", err); } console.log("data", data); });
本章目录
    1v1免费职业咨询