Nodejs Error: EMFILE

http://stackoverflow.com/q/13192660/2177408


I need to open a directory containing files.. open readstream for each and write the data from all the files into a single file. But I keep getting Error: EMFILE, open 'chunks/piece96.data'

My ulimit was 256 and I increased it to 1024. I have 127 files in the directory to open, read and write into a single file.

My code is below



    var DIR='chunks/';
    var files=fs.readdirSync(DIR);
    var filename='bach.mp3';

    files.forEach(function(singlebit){
        //console.log(files);
        var bit=fs.createReadStream(DIR+singlebit);
        var resultfile=fs.createWriteStream(filename,{
            flags:'r+',
            encoding:null,
            mode:0666
        });
        bit.on('data',function(bitdata){ 
                resultfile.write(bitdata); 
                console.log(bitdata);
            }).on('end',function(){
                resultfile.end();
            });
        });
    console.log('file complete');

How can I prevent the EMI file error. I am not opening many files at once since I am using readdirSync and not opening all of them at once. I need a way to read all the files and write to a single file


I changed the write stream to sync. That solved it

你可能感兴趣的:(Node.js)