[英]java: how to use bufferedreader to read specific line
Lets say I have a text file called: data.txt (contains 2000 lines) 假设我有一个名为的文本文件:data.txt(包含2000行)
How do I read given specific line from: 500-1500 and then 1500-2000 and display the output of specific line? 如何读取给定的特定行:500-1500然后1500-2000并显示特定行的输出?
this code will read whole files (2000 line) 此代码将读取整个文件(2000行)
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? 如何修改上面的代码以读取特定的行?
I suggest java.io.LineNumberReader. 我建议使用java.io.LineNumberReader。 It extends BufferedReader and you can use its
LineNumberReader.getLineNumber();
它扩展了BufferedReader,你可以使用它的
LineNumberReader.getLineNumber();
to get the current line number 获取当前行号
You can also use Java 7 java.nio.file.Files.readAllLines
which returns a List<String>
if it suits you better 您还可以使用Java 7
java.nio.file.Files.readAllLines
返回List<String>
如果它更适合您)
Note: 注意:
1) favour StringBuilder over StringBuffer, StringBuffer is just a legacy class 1)在StringBuffer上使用StringBuilder,StringBuffer只是一个遗留类
2) contents.append(System.getProperty("line.separator"))
does not look nice use contents.append(File.separator)
instead 2)
contents.append(System.getProperty("line.separator"))
看起来不太好使用contents.append(File.separator)
代替
3) Catching exception seems irrelevant, I would also suggest to change your code as 3)捕获异常似乎无关紧要,我还建议将代码更改为
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();
}
}
now code looks cleaner in my view. 现在代码看起来更干净了。 And if you are in Java 7 use try-with-resources
如果您使用Java 7,请使用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() };
}
}
Note that it returns 2 strings 500-1499 and 1500-2000 请注意,它返回2个字符串500-1499和1500-2000
A slightly more cleaner solution would be to use FileUtils in apache commons. 更简洁的解决方案是在apache commons中使用FileUtils。 http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html Example snippet:
http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html示例代码段:
String line = FileUtils.readLines(aFile).get(lineNumber);
The better way is to use BufferedReader. 更好的方法是使用BufferedReader。 If you want to read line 32 for example:
如果您想阅读第32行,例如:
for(int x = 0; x < 32; x++){
buf.readLine();
}
lineThreeTwo = buf.readLine();
Now in String lineThreeTwo you have stored line 32. 现在在String lineThreeTwo中你存储了第32行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.