java I/O系统 LineNumberReader类
LineNumbeReader类可以很方便的读取文件的行号
package ch13; import java.io.*; import io.BufferedInputFile; public class BasicFileOutput { static String file = "src/ch13/Writer.java"; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new StringReader( BufferedInputFile.read("src/ch13/Read.java"))); PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter(file))); LineNumberReader ln = new LineNumberReader(in); int lineCount = 1; String s; ln.setLineNumber(5); while((s = ln.readLine()) != null ) out.println(ln.getLineNumber() + ": "+ s); out.close(); // Show the stored file: System.out.println(BufferedInputFile.read(file)); } } /* 6: package ch13; 7: 8: import java.io.*; 9: import java.util.Iterator; 10: import java.util.LinkedList; 11: import java.util.ListIterator; 12: 13: public class Read { 14: public static void main(String[] args)throws IOException 15: { 16: if(args.length!=1) 17: System.exit(1); 18: BufferedReader in = new BufferedReader(new FileReader(args[0])); 19: LinkedList<String> list = new LinkedList<String>(); 20: String s; 21: StringBuilder sd = new StringBuilder(); 22: while(in.ready()) 23: { 24: s=in.readLine(); 25: list.add(s); 26: } 27: PrintWriter out = new PrintWriter( 28: new BufferedWriter(new FileWriter("../src/ch12/Writer.java"))); 29: Integer lineNumber = 1; 30: for(String str:list) 31: { 32: out.println((lineNumber++) + ": " + str); 33: } 34: out.close(); 35: } 36: } *///:~