我正试图读取一个文本文件,其中包含约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."
";
}
?>
请给我讲讲这个问题。
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.
8 seconds for that code sounds much too long to me. I suspect something else is going on, to be honest. Are you sure it's not console output which is taking a long time?
I suggest you time it (e.g. with System.nanoTime
) writing out the total time at the end, but run it with a console minimized. I suspect you'll find it's fast enough then.
Isn't that just the console output that is slow? Now that you know that you're file is read correctly, try by commenting out the line System.out.println(line);
.
file_get_contents loads all the file contents into a String, with your code in Java you are reading and printing line by line. If you are testing inside an IDE like Eclipse, the console output can be quite slow. If you want the exact behavior of file_get_contents, you can use this dirty code :
File f = new File(path, "sample.txt");
ByteArrayOutputStream bos = new ByteArrayOutputStream(new Long(Math.min(Integer.MAX_VALUE, f.length())).intValue());
FileInputStream fis = new FileInputStream(f);
byte[] buf = new byte[1024 * 8];
int size;
while((size = fis.read(buf)) > 0) {
bos.write(buf, 0, size);
}
fis.close();
bos.close();
System.out.println(new String(bos.toByteArray()));
Readers are usually slow, you should try Stream readers which are fast. And make sure that FIlE opening process is not taking time. If File is opened and stream objects are created and then measure time, then you can figure out exactly it is due to File opening issue or reading the file issue. Make sure that system io load is not high at the time of this operation, otherwise you measurement will go bad.
BufferedInputStream reader=new BufferedInputStream(new FileInputStream("/home/shantanu/Documents/sample.txt"));
byte[] line=new byte[1024];
while(reader.read(line)>0) {
System.out.println(new String(line));
}
Well if you r using readline it will go and read the file 1000 times for each line . Try using the read function with a very big buffer say over 28000 or so. It will then read a file say a total of 60 times for 1.4 MB which is much lesser than 1000. If u use a small buffer of 1000, then its gonna read the file around 1300 or something which is even slower than 1000( readline ). Also while printing the lines use print instead of println since the lines are not exactly lines but an array of characters.