iam正在获取缓冲区和十六进制代码


var fs = require('fs');
fs.readFile('TestFile.txt', function (err, data) {
if (err) throw err;
console.log(data);
});

//TestFile.txt This is test file to test fs module of Node.js

  1. 我正在获取缓冲区和十六进制代码来代替控制台数据
  2. <缓冲器54 68 69 73 20 69 73 20 74 65 73 20 66 69 6c 65 20 74 6f 20 74 73 20 66 73 20 6d 6f 64 75 6c 65 60 f 66 20 4e 6f 64 65 2e 6a 73>

根据文档:

如果未指定编码(使用options.encoding(,则数据将作为<Buffer>对象返回。否则,数据将是一个字符串。

解决方案:添加一个编码(并读取文档!:(

fs.readFile('TestFile.txt', { encoding : 'utf8' } , function()...

通过将数据转换为字符串(data(,您将获得文本

最新更新