nodejs模块——fs模块 读取文件

readFile读取文件

fs.readFile(filename,[option],callback) 方法读取文件。

参数说明:

  • filename String 文件名
  • option Object
    • encoding String |null default=null
    • flag String default='r'
  • callback Function

例子:

readfile.js

var fs = require('fs'); // 引入fs模块

fs.readFile('./text.txt', function(err, data) {
    // 读取文件失败/错误
    if (err) {
        throw err;
    }
    // 读取文件成功
     console.log('utf-8: ',data.toString());
});

  text.txt

helloworld
sunny

效果:

16:41:32 2017-10-20 

原文地址:https://www.cnblogs.com/guangzhou11/p/7700228.html