添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
小胡子的羽毛球  ·  windows ...·  1 年前    · 
稳重的啤酒  ·  [WPF] 资源, 静态资源, ...·  1 年前    · 
开心的棒棒糖  ·  java - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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'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
                Jul 31, 2022 at 11:17

For 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
                I have the same problem sometimes even with async fs.readFile().  The data is sometimes "" even though I can verify that the file  exists and has non-empty content by then doing a sync read in debugger.  This happens on Windows 10.
– Panu Logic
                Apr 17, 2020 at 18:09
                Could be used by the editor I'm editing it with. Not sure. But I found a get-around. If the content seems to be empty I do a sync-read in my callback function now and I get the  correct content always so far it seems
– Panu Logic
                Apr 18, 2020 at 23:45
                This solution works for me, same situation (files created with NodeJS 12) but I don't why it happens. Does somebody have a clue?
– Gorzas
                Dec 30, 2021 at 11:40
                Hey Michael.  I had actually tried both of these things previously and just did it again.  But I still get an empty string.  There must be some way to get information about the file descriptors or Buffers to debug this.  I'm about to try switching the node version.
– Vicky Vargas
                Aug 10, 2016 at 7:43
                are you sure your csvFds dictionary contains correct data? can you show what csvFds contains after for loop ends?
– mic4ael
                Aug 10, 2016 at 10:36
                Here's is what csvFds contains just before calling readFileSync:      { file1: 13,       file2: 14,       file3: 15,       file4: 16 }  My version of the code is actually using moment.js to format the date not now.getTime().  I think the file descriptors are fine after the initial loop because I am able to write multiple batches of data to these files without any issues.
– Vicky Vargas
                Aug 10, 2016 at 12:08
                It's a super simple csv file like this but it's a few thousand lines long each:           168722|admin        328701|developer        172090|mail        328702|team  Not sure why the line breaks don't work
– Vicky Vargas
                Aug 10, 2016 at 12:13
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.