java:如何使用bufferedreader读取特定的行 [英] java: how to use bufferedreader to read specific line
问题描述
假设我有一个名为的文本文件:data.txt(包含2000行)
Lets say I have a text file called: data.txt (contains 2000 lines)
如何读取给定的特定行:500-1500然后1500 -2000
并显示特定行的输出?
How do I read given specific line from: 500-1500 and then 1500-2000 and display the output of specific line?
此代码将读取整个文件(2000行)
this code will read whole files (2000 line)
public static String getContents(File aFile) {
StringBuffer contents = new StringBuffer();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
如何修改上述代码以读取特定行?
How do I modify above code to read specific line?
推荐答案
我建议使用java.io.LineNumberReader。它扩展了BufferedReader和
你可以使用它的 LineNumberReader.getLineNumber();
来获取当前行号
I suggest java.io.LineNumberReader. It extends BufferedReader and
you can use its LineNumberReader.getLineNumber();
to get the current line number
您还可以使用Java 7 java.nio.file.Files.readAllLines
,它返回 List< String>
如果它更适合你
You can also use Java 7 java.nio.file.Files.readAllLines
which returns a List<String>
if it suits you better
注意:
1)在StringBuffer上使用StringBuilder,StringBuffer只是一个遗留类
1) favour StringBuilder over StringBuffer, StringBuffer is just a legacy class
2) contents.append(System.getProperty(line.separator))
看起来不太好
使用 contents.append(File.separator)
而不是
2) contents.append(System.getProperty("line.separator"))
does not look nice
use contents.append(File.separator)
instead
3)捕捉异常似乎无关紧要,我会还建议将代码更改为
3) Catching exception seems irrelevant, I would also suggest to change your code as
public static String getContents(File aFile) throws IOException {
BufferedReader rdr = new BufferedReader(new FileReader("aFile"));
try {
StringBuilder sb = new StringBuilder();
// read your lines
return sb.toString();
} finally {
rdr.close();
}
}
现在代码看起来更干净了。如果你在Java 7中使用try-with-resources
now code looks cleaner in my view. And if you are in Java 7 use try-with-resources
try (BufferedReader rdr = new BufferedReader(new FileReader("aFile"))) {
StringBuilder sb = new StringBuilder();
// read your lines
return sb.toString();
}
所以最后你的代码看起来像
so finally your code could look like
public static String[] getContents(File aFile) throws IOException {
try (LineNumberReader rdr = new LineNumberReader(new FileReader(aFile))) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (String line = null; (line = rdr.readLine()) != null;) {
if (rdr.getLineNumber() >= 1500) {
sb2.append(line).append(File.pathSeparatorChar);
} else if (rdr.getLineNumber() > 500) {
sb1.append(line).append(File.pathSeparatorChar);
}
}
return new String[] { sb1.toString(), sb2.toString() };
}
}
请注意,它返回2个字符串500-1499和1500 -2000
Note that it returns 2 strings 500-1499 and 1500-2000
这篇关于java:如何使用bufferedreader读取特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!