码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-10-20 18:26:01      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:alt   方法   ges   span   images   9.png   fun   image   arp   

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 

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

标签:alt   方法   ges   span   images   9.png   fun   image   arp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!