zoukankan      html  css  js  c++  java
  • 读取文件内容

    /**
         * 读取文件特定行
         * @param type $filename
         * @param type $startLine
         * @param type $endLine
         * @param type $method
         * @return array
         */
        protected function getFileLines($filename, $startLine = 1, $endLine = 50, $method = 'rb') {
            $content = array();
            $count = $endLine - $startLine;
            $fp = fopen($filename, $method);
            if (!$fp)
                return $content;
            for ($i = 1; $i < $startLine; ++$i) {// 跳过前$startLine行
                fgets($fp);
            }
            for ($i = $startLine; $i <= $endLine; ++$i) {
                $content[] = fgets($fp); // 读取文件行内容
            }
            fclose($fp);
            $content = array_filter($content);  // array_filter过滤:false,null,''
            foreach ($content as $key => $value) {
                $content[$key] = json_decode($value, true);
            }
            return $content;
        }
    
        /**
         * 获取文件有多少行
         * @param type $file
         * @return type
         */
        protected function count_line($file) {
            $fp = fopen($file, "r");
            $i = 0;
            while (!feof($fp)) {
                //每次读取2M
                if ($data = fread($fp, 1024 * 1024 * 2)) {
                    //计算读取到的行数
                    $num = substr_count($data, "
    ");
                    $i+=$num;
                }
            }
            fclose($fp);
            return $i;
        }
  • 相关阅读:
    servlet程序开发
    jsp九大内置对象
    git原理教程
    jsp基础语法_Scriptlet_基本指令
    06_mysql多表查询
    05_mysql单表查询
    04_mysql增删改操作
    03_mysql索引的操作
    01_mysql数据库操作和基本数据类型
    生成器
  • 原文地址:https://www.cnblogs.com/bandbandme/p/6675030.html
Copyright © 2011-2022 走看看