逐行读取文件并每行 2 组字符串的最快方法?

发布于 2024-10-17 23:39:27 字数 509 浏览 3 评论 0原文

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

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

即使字符串之间有空格,我也总是需要两组字符串,例如“按行”

目前我正在使用

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

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

是否足够有效或者是否有使用标准的更有效方法JAVA API(请不要使用外部库)任何帮助谢谢!

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

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"

Currently I am using

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤带着心事 2024-10-24 23:39:27

这取决于你所说的“高效”是什么意思。从性能的角度来说还可以。如果您询问代码风格和大小,我个人会做一些小的修正:

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

对于从 STDIN 读取,Java 6 为您提供了另一种方式。使用 Console 类及其方法

readLine()

readLine(fmt, 对象... args)

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.
        }

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

readLine()
and
readLine(fmt, Object... args)

蓝色星空 2024-10-24 23:39:27
import java.util.*;
import java.io.*;
public class Netik {
    /* File text is
     * this, is
     * a, test,
     * of, the
     * scanner, I
     * wrote, for
     * Netik, on
     * Stack, Overflow
     */
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new File("test.txt"));
        sc.useDelimiter("(\\s|,)"); // this means whitespace or comma
        while(sc.hasNext()) {
            String next = sc.next();
            if(next.length() > 0)
                System.out.println(next);
        }
    }
}

结果:

C:\Documents and Settings\glowcoder\My Documents>java Netik
this
is
a
test
of
the
scanner
I
wrote
for
Netik
on
Stack
Overflow

C:\Documents and Settings\glowcoder\My Documents>
import java.util.*;
import java.io.*;
public class Netik {
    /* File text is
     * this, is
     * a, test,
     * of, the
     * scanner, I
     * wrote, for
     * Netik, on
     * Stack, Overflow
     */
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new File("test.txt"));
        sc.useDelimiter("(\\s|,)"); // this means whitespace or comma
        while(sc.hasNext()) {
            String next = sc.next();
            if(next.length() > 0)
                System.out.println(next);
        }
    }
}

The result:

C:\Documents and Settings\glowcoder\My Documents>java Netik
this
is
a
test
of
the
scanner
I
wrote
for
Netik
on
Stack
Overflow

C:\Documents and Settings\glowcoder\My Documents>
寂寞清仓 2024-10-24 23:39:27

如果你想要单独的两组字符串,你可以这样做:

BufferedReader in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
    String[] strArr = str.split(",");
    System.out.println(strArr[0] + " " + strArr[1]);
}
in.close();

If you want separate two sets of String you can do this in that way:

BufferedReader in = new BufferedReader(new FileReader(file));
String str;
while ((str = in.readLine()) != null) {
    String[] strArr = str.split(",");
    System.out.println(strArr[0] + " " + strArr[1]);
}
in.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文