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

使用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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集