Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - Fastest way to read a file line by line with 2 sets of Strings on each line?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...