node.js读取文件

1.新建test.txt文件,输入中文,保存为utf-8格式

2.新建test.js,引入fs模块,使用fs.readFile(dir,function(err,data){}) ,把输出的内容使用toString()

// 引入fs 模块 (file system)
var fs = require('fs')
// fs 读取文件方法 fs.readFile('路径',function(err,data){})
fs.readFile('./test.txt',function(err,data){
    console.log(data.toString())
})

结果:

猜你喜欢

转载自www.cnblogs.com/luguankun/p/12585495.html