逐行读取文件的最快方法是每行有2组字符串? [英] Fastest way to read a file line by line with 2 sets of Strings on each line?

查看:88
本文介绍了逐行读取文件的最快方法是每行有2组字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于包含两个字符串的每一行,我可以逐行读取的最快方法是什么。
一个示例输入文件是:

What is the fastest way I can read line by line with each line containing two Strings. An example input file would be:

Fastest, Way
To, Read
One, File
Line, By Line
.... can be a large file

即使String之间有空格,每条线上也总有两组字符串按行

There are always two sets of strings on each line that I need even if there are spaces between the String e.g. "By Line"

目前我正在使用

FileReader a = new FileReader(file);
            BufferedReader br = new BufferedReader(a);
            String line;
            line = br.readLine();

            long b = System.currentTimeMillis();
            while(line != null){

这是否足够有效还是有更多使用标准JAVA API的有效方式(请不要使用外部库)任何帮助表示赞赏谢谢!

Is that efficient enough or is there a more efficient way using standard JAVA API (no outside libraries please) Any help is appreciated Thanks!

推荐答案

这取决于你的意思你说有效率。从性能的角度来看,这是可以的。如果你问的是代码风格和大小,我几乎可以做一个小的修正:

It depends what do you mean when you say "efficient." From the point of view of performance it is OK. If you are asking about the code style and size, I pesonally do almost you do with a small correction:

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null) {
             // do something with line.
        }

从STDIN读取Java 6为您提供了另一种方式。使用类控制台及其方法

For reading from STDIN Java 6 offers you yet another way. Use class Console and its methods

readLine()

readLine(fmt,Object ... args)

这篇关于逐行读取文件的最快方法是每行有2组字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆