为什么BufferedReader在读取PHP文件时会跳过一些行?

时间:2013-07-03 16:52:40

标签: java inputstream readfile

我正在用Java编写一个函数来逐行读取PHP。我注意到有些行被跳过了。

Java中的函数:

 public String ReadContentRegex(String path, String Regex) throws
 FileNotFoundException, IOException
     {
         final StringBuilder contents = new StringBuilder();

         BufferedReader br = new BufferedReader(

         new InputStreamReader(new FileInputStream(path)));

         try {
             String line;

             while ((line = br.readLine()) != null) {
                 System.out.println(br.readLine());

             }
         } finally {
             br.close();
         }

         return contents.toString();

     }

Java应该使用BufferedReader读取的PHP文件:

<?php #if (!APP_ACT) exit();

class dbtest extends Codeplexon\core\fwcontroller {

public $name = __CLASS__;

    static function destruct()
{
    // ili 404
    echo("Error function: ".__FUNCTION__);
}

public function db()
{       
        $model = new Codeplexon\model\fwmodel;
        $model->db->load();
        #$e = $model->db->count();
        $model->db->select();
        $model->db->from("db");
        $model->db->query();
        $a = $model->db->fetch();
        #$num_rows = $model->db->numrows();
        #$lastquery = $model->db->lastquery();

        var_dump($a);
}
    //etc....

输出错误:

class dbtest extends Codeplexon\core\fwcontroller {

        // ili 404

            $model = new Codeplexon\model\fwmodel;
            $model->db->select();
            $a = $model->db->fetch();


            $model = new Codeplexon\model\fwmodel;
            $obj = array("val" => substr(md5(time()), 0, 10));
        }
        {
            $model->db->load();
            $model->db->where("id", "5");

            $model = new Codeplexon\model\fwmodel;
            $r = $model->db->querycustom('SELECT * FROM a_mvcdb.db WHERE id = 4');

        }
        {

我认为返回语句.toString()或类名public String ReadContentRegex(String path, String Regex)存在问题,但我不确定是什么问题。

1 个答案:

答案 0 :(得分:5)

您正在拨打readLine()两次。

您读取一行并将其存储在String变量line中:

while ((line = br.readLine()) != null) {

然后你忽略你所读到的内容,并在println语句中读到另一个行。

System.out.println(br.readLine());

将以上行更改为

System.out.println(line);