使用Java查找文件中有多少非空行的最快方法是什么?
答案 0 :(得分:6)
最简单的方法是使用BufferedReader,并检查哪些行为空。但是,这是一种相对较慢的方法,因为它需要为文件中的每一行创建一个String对象。更快的方法是使用read()将文件读入数组,然后遍历数组以计算换行符。
这是两个选项的代码;第二个占用了我机器上大约50%的时间。
public static void timeBufferedReader () throws IOException
{
long bef = System.currentTimeMillis ();
// The reader buffer size is the same as the array size I use in the other function
BufferedReader reader = new BufferedReader(new FileReader("test.txt"), 1024 * 10);
int counter = 0;
while (reader.ready())
{
if (reader.readLine().length() > 0)
counter++;
}
long after = System.currentTimeMillis() - bef;
System.out.println("Time: " + after + " Result: " + counter);
}
public static void timeFileReader () throws IOException
{
long bef = System.currentTimeMillis();
FileReader reader = new FileReader("test.txt");
char[] buf = new char[1024 * 10];
boolean emptyLine = true;
int counter = 0;
while (reader.ready())
{
int len = reader.read(buf,0,buf.length);
for (int i = 0; i < len; i++)
{
if (buf[i] == '\r' || buf[i] == '\n')
{
if (!emptyLine)
{
counter += 1;
emptyLine = true;
}
}
else emptyLine = false;
}
}
long after = System.currentTimeMillis() - bef;
System.out.println("Time: " + after + " Result: " + counter);
}
答案 1 :(得分:5)
我和Limbic System就NIO的建议。我已经为Daphna的测试代码添加了一个NIO方法,并且用他的两种方法对它进行了标记:
public static void timeNioReader () throws IOException {
long bef = System.currentTimeMillis();
File file = new File("/Users/stu/test.txt");
FileChannel fc = (new FileInputStream(file)).getChannel();
MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, 0, file.length());
boolean emptyLine = true;
int counter = 0;
while (buf.hasRemaining())
{
byte element = buf.get();
if (element == '\r' || element == '\n') {
if (!emptyLine) {
counter += 1;
emptyLine = true;
}
} else
emptyLine = false;
}
long after = System.currentTimeMillis() - bef;
System.out.println("timeNioReader Time: " + after + " Result: " + counter);
}
以下是89MB文件的预热结果:
timeBufferedReader Time: 947 Result: 747656
timeFileReader Time: 670 Result: 747656
timeNioReader Time: 251 Result: 747656
NIO比FileReader快2.5倍,比BufferedReader快4倍!
使用6.4MB文件时,结果会更好,但预热时间要长得多。
//jvm start, warming up
timeBufferedReader Time: 121 Result: 53404
timeFileReader Time: 65 Result: 53404
timeNioReader Time: 40 Result: 53404
//still warming up
timeBufferedReader Time: 107 Result: 53404
timeFileReader Time: 60 Result: 53404
timeNioReader Time: 20 Result: 53404
//ripping along
timeBufferedReader Time: 79 Result: 53404
timeFileReader Time: 56 Result: 53404
timeNioReader Time: 16 Result: 53404
随心所欲。
答案 2 :(得分:2)
最简单的是使用扫描仪(是的,我喜欢详细的代码......你可以让它更短一些)。扫描仪()还需要文件,阅读器等...所以你可以随心所欲地传递它。
import java.util.Scanner;
public class Main
{
public static void main(final String[] argv)
{
final Scanner scanner;
final int lines;
scanner = new Scanner("Hello\n\n\nEvil\n\nWorld");
lines = countLines(scanner);
System.out.println("lines = " + lines);
}
private static int countLines(final Scanner scanner)
{
int lines;
lines = 0;
while(scanner.hasNextLine())
{
final String line;
line = scanner.nextLine();
if(line.length() > 0)
{
lines++;
}
}
return lines;
}
}
答案 3 :(得分:2)
如果它确实是最快的,你应该研究NIO。然后,在您的目标平台上测试您的代码,看看使用NIO是否真的更好。我在为Netflix Prize玩的一些代码中得到了一个数量级的改进。它涉及将数千个文件解析为更紧凑,快速加载的二进制格式。 NIO对我的(慢速)开发笔记本电脑有很大的帮助。