逐行读取文件并每行 2 组字符串的最快方法?
我可以逐行读取每行包含两个字符串的最快方法是什么。 一个示例输入文件是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于你所说的“高效”是什么意思。从性能的角度来说还可以。如果您询问代码风格和大小,我个人会做一些小的修正:
对于从 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:
For reading from STDIN Java 6 offers you yet another way. Use class Console and its methods
readLine()
and
readLine(fmt, Object... args)
结果:
The result:
如果你想要单独的两组字符串,你可以这样做:
If you want separate two sets of String you can do this in that way: