Bixiangdong _ Java basic video tutorial 19th _ IO stream (06 ~ 10), _ java_io

Source: Internet
Author: User

Bixiangdong _ Java basic video tutorial 19th _ IO stream (06 ~ 10), _ java_io
19th days-06-IO stream (decorative design mode)

  • Decoration Design Mode: When you want to enhance the functions of an existing object, you can define a class, pass in existing objects, and provide enhanced functions based on existing functions. The custom class is called the decoration class.
  • Decoration Classes usually receive decorated objects through constructor. And provides stronger functions based on the functions of the objects to be decorated.
19th days-07-IO stream (differences between decoration and inheritance)
MyReader // class specifically used to read data
| ---- MyTextReader
| ---- MyMediaReader
| ---- MyDataReader
|____ MyBufferReader

Class MyBufferReader {
MyBufferReader (MyTextReader text ){}
MyBufferReader (MyMediaReader media ){}
MyBufferReader (MyDataReader data ){}
...
}

The above pattern class has poor scalability. You can find the common types of its parameters and improve scalability through polymorphism.
Class MyBufferReader extends MyReader {
Private MyReader r;
MyBufferReader (MyReader r ){}
...
}

The decoration mode is more flexible than inheritance, avoiding the bloated inheritance system and reducing the relationship between classes (inheritance Structure --> combination structure ).
Because the decoration class enhances existing objects and has the same functions as existing classes, it only provides stronger functions. Therefore, the decoration class and the decorated class generally belong to the same system.
19th days-08-IO stream (custom decoration class)
Import java. io. fileReader; import java. io. IOException; import java. io. reader; class MyBufferedReader extends Reader {private Reader r; MyBufferedReader (Reader r) {this. r = r ;}// you can read a row of data at a time. Public String myReadLine () throws IOException {// defines a temporary container. The original BufferReader encapsulates character arrays. For ease of demonstration. Define a StringBuilder container. Because the data will eventually be converted into a string. StringBuilder sb = new StringBuilder (); int ch; while (ch = r. read ())! =-1) {if (ch = '\ R') continue; if (ch =' \ n') {return sb. toString ();} else {sb. append (char) ch) ;}} if (sb. length ()! = 0) return sb. toString (); return null;}/* overwrites the abstract method in the Reader class. Read (char [] cbuf, int off, int len) and close () are the abstract methods of Reader and must be implemented. */Public int read (char [] cbuf, int off, int len) throws IOException {return r. read (cbuf, off, len);} public void close () throws IOException {r. close ();} public void myClose () throws IOException {r. close () ;}} class MyBufferedReaderDemo {public static void main (String [] args) throws IOException {FileReader fr = new FileReader ("buf.txt "); myBufferedReader myBuf = new MyBufferedReader (fr); Str Ing line; while (line = myBuf. myReadLine ())! = Null) {System. out. println (line) ;}mybuf. myClose ();}}
19th-09-IO stream (LineNumberReader)
Import java. io. fileReader; import java. io. IOException; import java. io. lineNumberReader; public class LineNumberReaderDemo {public static void main (String [] args) throws IOException {FileReader fr = new FileReader ("buf.txt"); LineNumberReader lnr = new LineNumberReader (fr ); string line; lnr. setLineNumber (2); // set the current row number. Parameter: lineNumber-int value of the specified row number while (line = lnr. readLine ())! = Null) {System. out. println (lnr. getLineNumber () + ":" + line) ;}lnr. close ();}}

3: abcde1 # The row number starts from the next digit 3 of setLineNumber.
4: abcde2
5: abcde3
6: abcde4

19th-10-IO stream (MyLineNumberReader)
import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class MyLineNumberReader extends MyBufferedReader {    private int lineNumber;    public MyLineNumberReader(Reader r) {        super(r);    }    public void setLineNumber(int lineNumber) {        this.lineNumber = lineNumber;    }    public int getLineNumber() {        return lineNumber;    }    public String myReadLine() throws IOException {        lineNumber++;        return super.myReadLine();    }}class MyLineNumberReaderDemo {    public static void main(String[] args) throws IOException {        FileReader fr = new FileReader("buf.txt");        MyLineNumberReader mylnr = new MyLineNumberReader(fr);        mylnr.setLineNumber(200);        String line;        while ((line = mylnr.myReadLine()) != null) {            System.out.println(mylnr.getLineNumber() + "::" + line);        }        mylnr.myClose();    }}

 

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.