Reputation: 29507
I'm trying to read the content of test.txt
(which is on the same folder of the Javascript source) and display it using this code:
var fs = require("fs");
fs.readFile("test.txt", function (err, data) {
if (err) throw err;
console.log(data);
});
The content of the test.txt
was created on nano
:
Testing Node.js readFile()
And I'm getting this:
Nathan-Camposs-MacBook-Pro:node_test Nathan$ node main.js
<Buffer 54 65 73 74 69 6e 67 20 4e 6f 64 65 2e 6a 73 20 72 65 61 64 46 69 6c 65 28 29>
Nathan-Camposs-MacBook-Pro:node_test Nathan$
Upvotes: 543
Views: 415174
Reputation: 11
Basically it does read you data, butit reads the raw data which need to be changed to another format like "utf8" to make sense try using :
const data = fs.readFile("input.txt","utf8",function (err,data){
if(err){
return err
}
console.log(data);
})
Upvotes: 0
Reputation: 47
It is just a function to return content, to encode that you need to add encoding parameter such as "UTF-8"
Upvotes: -1
Reputation: 484
The data
variable contains a Buffer
object. Convert it into ASCII encoding using the following syntax:
data = data.toString('ascii', 0, data.length)
Or to UTF-8
encoding:
data = data.toString('utf8', 0, data.length)
Asynchronously:
fs.readFile('test.txt', 'utf8', function (error, data) {
if (error) throw error;
console.log(data.toString());
});
Upvotes: 22
Reputation: 14735
This comes up high on Google, so I'd like to add some contextual information about the original question (emphasis mine):
Why does Node.js' fs.readFile() return a buffer instead of string?
Even if you as the programmer know it: Node has no idea what's in the file you're trying to read. It could be a text file, but it could just as well be a ZIP archive or a JPG image — Node doesn't know.
Even if Node knew it were to read a text file, it still would have no idea which character encoding is used (i.e. how the bytes in the file map to human-readable characters), because the character encoding itself is not stored in the file.
There are ways to guess the character encoding of text files with more or less confidence (that's what text editors do when opening a file), but you usually don't want your code to rely on guesses without your explicit instruction.
So, because it does not and can not know all these details, Node just reads the file byte for byte, without assuming anything about its contents.
And that's what the returned buffer is: an unopinionated container for the raw bytes in the file. How these bytes should be interpreted is up to you as the developer.
Upvotes: 146
Reputation: 2430
You're missing the encoding scheme at the second parameter, which is usually be "utf-8"
. Plain buffer is returned if no coding scheme is mentioned.
Upvotes: 1
Reputation: 120318
Try:
fs.readFile("test.txt", "utf8", function(err, data) {...});
Basically, you need to specify the encoding.
Upvotes: 199
Reputation: 45575
From the docs:
If no encoding is specified, then the raw buffer is returned.
Which might explain the <Buffer ...>
. Specify a valid encoding, for example utf-8
, as your second parameter after the filename. Such as,
fs.readFile("test.txt", "utf8", function(err, data) {...});
Upvotes: 768
Reputation: 2985
Async:
fs.readFile('test.txt', 'utf8', callback);
Sync:
var content = fs.readFileSync('test.txt', 'utf8');
Upvotes: 62
Reputation: 2258
It is returning a Buffer object.
If you want it in a string, you can convert it with data.toString()
:
var fs = require("fs");
fs.readFile("test.txt", function (err, data) {
if (err) throw err;
console.log(data.toString());
});
Upvotes: 48