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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值