前段时间需要读报表打印,需求是可以从第N行读到第N行,发现百度出来的LineNumberReader方法都不怎么理想
然后就找到一篇和众百度里脱颖而出的好文章,所以分享之:
从类 的命名来看,貌似LineNumberReader可以支持从任意行读取的功能,并且提供了setLineNumber()的方法,但是这个按照并不能改变文件指针的位置,只是你调用getLineNumber()这个函数时,结果会变。API上面说它并不能改变文件物理的指针位置。看来以后得小心使用。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class Ex7 {
public void dataReader(String nameFile, int start, int finish) {
if (start > finish) {
System.out.println("Error start or finish!");
return;
}
InputStream inputStream = null;
LineNumberReader reader = null;
try {
inputStream = new FileInputStream(new File(nameFile));
reader = new LineNumberReader(
new InputStreamReader(inputStream));
int lines = getTotalLines(new File(nameFile));
if (start < 0 || finish < 0 || finish > lines || start > lines) {
System.out.println("Line not found!");
return;
}
String line = reader.readLine();
lines = 0;
while (line != null) {
lines++;
if(lines >= start && lines <= finish){
System.out.println(line);
}
line = reader.readLine();
}
inputStream.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO Error");
System.exit(0);
}
}
private int getTotalLines(File file) throws IOException{
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
String line = reader.readLine();
int lines = 0;
while (line != null) {
lines++;
line = reader.readLine();
}
reader.close();
in.close();
return lines;
}
public static void main(String[] args) {
new Ex7().dataReader("data.txt", 2, 4);
new Ex7().dataReader("data.txt",3,8);
}
}