1.前言。
要保存当前读的记录,可以用java的类LineNumberReader.里面有getLineNumber()方法可以获得行数。
2.例子。
package com.redis; 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); //记录当前行数 System.out.println("读到第:"+reader.getLineNumber()+"行"); } 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("c:/data.txt", 2, 4); //new Ex7().dataReader("data.txt",3,8); } }
3.后记.
例子程序是在http://www.myexception.cn/j2se/267711.html和http://asialee.iteye.com/blog/368489的基础上修改得还,感谢这两位无私的小编和楼主。