Java adds line number styles to ReadLine extensions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.