In the use of Java streams, in the character buffer input stream, there is a way to read a row of data each time: ReadLine ();
Here, the simple inheritance method is used to extend it so that the ordinal is added before each line is returned
1 2Some packages that need to be imported3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 ImportJava.io.FileReader;6 Importjava.io.IOException;7 ImportJava.io.Reader;8 9 //use inherited methods to extend the ReadLine () method of the BufferedReader classTen One //Customizing a class that inherits BufferedReader A classBufferedrlnextendsbufferedreader{ - intCount = 0; - //constructor method, using the parameters of the parent class the PublicBufferedrln (Reader in) { - Super(in);// - } - + @Override - PublicString ReadLine ()throwsIOException { + //Call the ReadLine () method of the parent class to return a row of data AString content =Super. ReadLine (); at //set the style for the returned data (add line numbers here) - if(Content = =NULL) { - return NULL;//if empty, the data has been read out - } -Content = count+ "" +content;//Add line number -count++; in returncontent; - } to } + - the Public classReadLine { * Public Static voidMain (string[] args)throwsIOException { $ //TODO auto-generated Method StubPanax NotoginsengFile File =NewFile ("C:\\users\\lx\\desktop\\io job. txt"); - the readlineExt1 (file); + } A the //using a custom class Bufferedrln + Public Static voidReadlineExt1 (File file)throwsioexception{ - $FileReader FRD =Newfilereader (file); $ //the actual parameter FRD will be passed to the constructor in the custom. -Bufferedrln BFRD =NewBufferedrln (FRD); - //reading Data theString string =NULL; - while(String = Bfrd.readline ())! =NULL) {Wuyi System.out.println (string); the } - //Close Resource Wu frd.close (); - } About $
Java adds line number styles to ReadLine extensions