只从输入中读取某些行? [英] Reading certain lines only from input?

查看:115
本文介绍了只从输入中读取某些行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些代码如 input = new BufferedReader(new FileReader(args [0])); 并且输入文件包含成对的行,我该怎么办?这样只导入每行的第一行?换句话说,每个奇数行只?
谢谢

If i have some code such as input = new BufferedReader(new FileReader(args[0])); And the input file contain pairs of lines, how can I make it so only the first line from each line is imported? So in other words, every odd numbered line only? Thanks

推荐答案

您可能想考虑使用 java.io.LineNumberReader 更简单地过滤奇数行( lineNo%2 == 1 )。或者在另一种方法中,如果您使用的是JDK7,则可以使用 java.nio.files.Files.readAllLines()方法,并在迭代时再次过滤奇数。

You might like to consider the use of java.io.LineNumberReader to make the filtering of odd lines (lineNo % 2 == 1) simpler. Or in an alternative approach, if you are using JDK7, you could use the java.nio.files.Files.readAllLines() method and again filter the odd ones upon iteration.

这篇关于只从输入中读取某些行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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