我正在尝试使用node.js中的“ fs”模块读取文件,如下所示:
var fs=require("fs");
fs.read("E:/Node.js/readme.txt","utf8",function(err,data){
console.log(data);
});
但是出现以下错误:
fs.js:664
binding.read(fd, buffer, offset, length, position, req);
^
TypeError: fd must be a file descriptor
at Object.fs.read (fs.js:664:11)
at Object.<anonymous> (E:\Node.js\First.js:2:4)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
为什么会这样?
答案 0 :(得分:0)
您应该使用readFile
而不是read
,read
用于从文件中部分读取字节
fs.readFile("E:\\Node.js\\readme.txt",'utf8', function(err,data){
console.log(data);
});