读大文件 边读边操作

class DeviceIdGenerator

{

    const CHUCK_SIZE = 4096;

    private $path;

    public function __construct(string $path)

{

        $this->path = $path;

}

    public function read()

{

        $buffer = '';

        $handle = fopen($this->path, 'r+');

        try {

            do {

                $next = fread($handle, self::CHUCK_SIZE);

                if ($next === '') {

                    break;

}

                $next = $buffer . $next;

                $parts = preg_split("/(\r|\n|\r\n|,)/", $next);

                $buffer = array_pop($parts);

                foreach ($parts as $part) {

                    $part && yield $part;

}

            } while (true);

        } finally {

            fclose($handle);

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,156评论 0 2
  • 本篇文章是基于谷歌有关Graphic的一篇概览文章的翻译:http://source.android.com/de...
    lee_3do阅读 7,478评论 2 21
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,229评论 0 3
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,996评论 0 0
  • 每当午后,莲子总是特别感到容易疲倦,就像刚睡醒似的,昏昏沉沉不想动弹,所以基本上每天午饭后都会拿出午睡床在办公室小...
    夏夏自留地阅读 297评论 0 0

友情链接更多精彩内容