在Nodejs中读取文件显示0字节



当我尝试读取Nodejs中的文件时,它显示0字节。

代码:

fs.readFile("thisArticle.html", 'utf8', function (err, data) {
if (err) {
return console.log(err);
}

console.log(data.byteLength); // Displays 0 bytes


});

同样,当我尝试这种方式时,同样的问题是它显示0字节。

var stats = fs.statSync("thisArticle.html", 'utf8');
var fileSizeInBytes = stats.size;
var fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
console.log(fileSizeInMegabytes); // Displays 0 bytes

我想知道我是否遗漏了什么,因为文件的路径是正确的,我不知道是代码本身还是HTML文件,尤其是

尝试此代码,您将在控制台中获得html文件

var fs= require('fs');
fs.readFile('./index.html', function(err, data){
if (err) {
return console.log(err);
}
console.log('test file is loaded:n',data.toString());
}); 

最新更新