dongyun234854 2013-05-29 07:28 采纳率: 0%
浏览 1994

使用BufferedReader.readLine()读取文本文件太慢了

我正试图读取一个文本文件,其中包含约1000行。整个文件大约1.4 MB。我使用 BufferedReader 的 readLine 方法读取文件,在控制台上打印输出需要8-10秒。我尝试使用了 fgets 的 php 和它读取所有相同的行,几乎在眨眼之间就实现了! 这怎么可能? 下面是我正在使用的代码:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClickLogDataImporter {
    public static void main(String [] args) {
        try {
            new ClickLogDataImporter().getFileData();
        } catch (Exception ex) {
            Logger.getLogger(ClickLogDataImporter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getFileData() throws FileNotFoundException, IOException {
        String path = "/home/shantanu/Documents";
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(path+"/sample.txt")));
        String line = "";
        while((line = (br.readLine())) != null) {
            System.out.println(line);
        }

    }
}

PHP 代码:

<?php
    $fileName = "/home/shantanu/Documents/sample.txt";
    $file = fopen($fileName, 'r');
    while(($line = fgets($file)) != false) {
        echo $line."
";
    }
?>

请给我讲讲这个问题。

  • 写回答

6条回答 默认 最新

  • dqsong2010 2013-05-29 07:32
    关注

    I'm not sure but I think PHP just prints the file given the method you used, Java reads the file and gets every lines from it, that means checking every character for a line breaker, the process does not seem to be the same at all.

    string file_get_contents
    

    If you try and print each line one by one from the file with PHP, it should be slower.

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀