admin管理员组文章数量:1355542
I have a script that writes data from an API to some files. I have an object the contains the file descriptors for each file:
var csvFds = {
'file1' : null,
'file2' : null,
'file3' : null,
'file4' : null
};
for (var file in csvFds) {
var dirPath = __dirname + '/files/' + file;
try {
fs.statSync(dirPath);
}
catch (e) {
mkdirp.sync(dirPath, {mode: 0755});
}
csvFds[file] = fs.openSync(dirPath + '/' + moment().format("YYYY-MM-DDTHH:mm:ss[Z]") + '.csv', 'a+');
}
Then I have some code that uses fs.write to write lines of csv to the file in batches. This part is working fine. I have well formed csv files. Now I need to read the contents of the entire file as a string. This is how I'm trying to do it:
fs.readFileSync(csvFds['file1']).toString();
But for some reason I am always getting an empty string. I have confirmed that fs.readFileSync is in fact returning a Buffer by using console.log and dropping the toString() method.
I'm really stuck on this so any help will be greatly appreciated. Thanks in advance. Here's some additional info regarding my node version and OS:
$ node -v
v6.2.3-pre
$ uname -a
Darwin i-2.local 14.5.0 Darwin Kernel Version 14.5.0: Thu Jun 16 19:58:21 PDT 2016; root:xnu-2782.50.4~1/RELEASE_X86_64 x86_64
I have a script that writes data from an API to some files. I have an object the contains the file descriptors for each file:
var csvFds = {
'file1' : null,
'file2' : null,
'file3' : null,
'file4' : null
};
for (var file in csvFds) {
var dirPath = __dirname + '/files/' + file;
try {
fs.statSync(dirPath);
}
catch (e) {
mkdirp.sync(dirPath, {mode: 0755});
}
csvFds[file] = fs.openSync(dirPath + '/' + moment().format("YYYY-MM-DDTHH:mm:ss[Z]") + '.csv', 'a+');
}
Then I have some code that uses fs.write to write lines of csv to the file in batches. This part is working fine. I have well formed csv files. Now I need to read the contents of the entire file as a string. This is how I'm trying to do it:
fs.readFileSync(csvFds['file1']).toString();
But for some reason I am always getting an empty string. I have confirmed that fs.readFileSync is in fact returning a Buffer by using console.log and dropping the toString() method.
I'm really stuck on this so any help will be greatly appreciated. Thanks in advance. Here's some additional info regarding my node version and OS:
$ node -v
v6.2.3-pre
$ uname -a
Darwin i-2.local 14.5.0 Darwin Kernel Version 14.5.0: Thu Jun 16 19:58:21 PDT 2016; root:xnu-2782.50.4~1/RELEASE_X86_64 x86_64
Share
Improve this question
edited Aug 10, 2016 at 12:11
Vicky Vargas
asked Aug 9, 2016 at 20:12
Vicky VargasVicky Vargas
531 gold badge1 silver badge6 bronze badges
2
-
if you provide encoding to
readFileSync
method you will get a string instead of aBuffer
– mic4ael Commented Aug 9, 2016 at 20:15 -
I've just discovered that a file with
[]
, as in an empty array serialized, is read with readFileSync it returns an empty string and length=0. I've tried returning as buffer and inspecting, it's the same. on macOS + Node v16.16. – Janaka Commented Jul 31, 2022 at 11:17
2 Answers
Reset to default 4For others who have the same problem.
For me, the only way to have a non-empty string was to use fs.readFile instead of fs.readFileSync.
I use a Mac and I was trying to read a file that node create itself. If I try to read another file it works.
fs.readFile(file, (err, data)=>{
if(err){
console.log(err)
throw err
}else{
let file_content = data.toString('utf8')
// your code here
}
})
Try to call readFileSync
like this readFileSync(csvFds['file1'], 'utf-8')
. It ought to return a string. Or you can omit the argument and then provide the encoding when calling toString
method e.g. readFileSync(csvFds['file1']).toString('utf-8')
本文标签: javascriptfsreadFileSync always returns empty stringStack Overflow
版权声明:本文标题:javascript - fs.readFileSync always returns empty string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744048858a2582043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论