Java Learning Lesson 49th-io Flow (iii): Buffer 2 & Decorative design mode

Source: Internet
Author: User

First, Analog BufferedReader

Custom Mybuffereareader

In the case of buffers, it is in fact encapsulated an array, providing methods for the operation of the array, the final operation of these methods are arrays of the angle label

Principle: Take the data from the source into the buffer, and then from the buffer constantly fetch the data, after the completion, continue to fetch data from the source, into the buffer, until the source of the data, with 1 as the end tag

Import Java.io.*;class Mybufferedreader {private filereader fr;//definition array as buffer private char[] ch = new char[1024];// Defines a pointer to the operation of an array, and when the last element of the operand is manipulated, the pointer is zero private int pos = 0;//defines a counter that, when the buffer's data is reduced to 0, continues to fetch data from the source into the buffer private int count = 0; Mybufferedreader (FileReader fr) {this.fr = fr;} public int Myread () throws ioexception{//gets a batch of data from the source into the buffer, first determine if Count is 0if (count==0) {count = Fr.read (ch);p OS = 0;} if (count<0) Return-1;char temp = Ch[pos++];count--;return temp;}  Public String Myreadline () throws ioexception{//stores the read data into the buffer StringBuilder SB = new StringBuilder (); int ch = 0;while ((ch = Myread ())!=-1) {if (ch== ' \ R ') continue;if (ch== ' \ n ') return sb.tostring (); Sb.append ((char) ch);} if (Sb.length ()!=0) return sb.tostring (); return null;} public void Myclose () throws Ioexception{fr.close ();}} public class Main {public static void main (string[] args) throws IOException {FileReader fr = new FileReader ("ACM.txt"); Mybufferedreader MSB = new Mybufferedreader (FR); String str = null;while (str = MSB. Myreadline ())!=null) {System.Out.println (str);} Msb.myclose ();}}


Second, decorative design mode

When the function of a group of objects is enhanced, you can use this mode to solve problems, the above-mentioned custom Mybufferedreader class is very good embodiment, for the FileReader analogy original function is simple, of course, the above code is relatively simple to write

Class room{public void Show () {System.out.println ("House");}} Class Newroom {private Or;public newroom (Oldroom or) {//TODO auto-generated constructor stubthis.or = or;} public void Show () {or.show (); SYSTEM.OUT.PRINTLN ("Cottage");}}

It is important to avoid the modification of the source code, of course, inheritance can also achieve the enhancement of functionality.

But inheriting the word will make the succession system complex and cumbersome.


The difference between decorative design patterns and inheritance:

Inheritance: If in order to enhance the function, it is necessary to continue to write subclasses, just for a new newroom, it is necessary to create a subclass, Newroom after the villa, but also to create sub-class, gradually, resulting in a system of inheritance more and more bloated

Decorative design mode: Whether Newroom or villa, are R a kind of oom that keeps the basic properties of the house

Therefore, the use of decorative design mode, only need to put the object of the decoration can be, buffer technology can be extracted separately for encapsulation, to buffer who will be associated with the buffer, the design of the relative system will become simple.


Third, LineNumberReader

API documentation explains: The buffer character input stream that tracks line numbers. This class defines methods setLineNumber(int) and getLineNumber() they can be used to set and get the current line number, respectively.

Import java.io.*;p Ublic class Main {public static void main (string[] args) throws IOException {FileReader fr = new Filerea Der ("ACM.txt"); LineNumberReader lr = new LineNumberReader (FR); String str = null;lr.setlinenumber (10);//Set line number starting with 10 while ((str = lr.readline ())!=null) {System.out.println (str+ "lines:" +lr.getlinenumber ());} Lr.close ();}}

This kind of use is not a lot of places, know how to use.


Iv. Practice

Be sure to copy the media file with a byte stream file

Import java.io.*;p Ublic class Main {public static void main (string[] args) throws IOException {//copy_mp3_1 ();//copy_mp3_ 2 ();//Development recommends using this copy_mp3_3 ();} public static void Copy_mp3_3 () throws IOException {//TODO auto-generated method Stubfileinputstream fis = new fileinputs Tream ("D:\\ Midsummer Light-Years mp3"); FileOutputStream fos = new FileOutputStream ("D:\\ Midsummer Light-year 3.mp3"); byte[] by = new Byte[fis.available ()];//limitation: File too large, Fis.read (by), Fos.write (by); Fos.close (); Fis.close (); public static void Copy_mp3_2 () throws IOException {//TODO auto-generated method Stubfileinputstream fis = new fileinputs Tream ("D:\\ Midsummer Light-Years mp3"); Bufferedinputstream br = new Bufferedinputstream (FIS); FileOutputStream fos = new FileOutputStream ("D:\\ Midsummer Light-year 2.mp3"); Bufferedoutputstream bw = new Bufferedoutputstream (FOS), int len = 0;while (len = Br.read ())!=-1) {bw.write (len); Bw.flush ();} Br.close (); Bw.close ();} public static void Copy_mp3_1 () throws IOException {//TODO auto-generated method Stubfileinputstream fis = new fileinputs Tream ("D:\\ Midsummer Light-Years. mp3 "); FileOutputStream fos = new FileOutputStream ("D:\\ Midsummer Light-year 1.mp3"); byte[] by = new byte[1024];//Note the number of bytes in the Mp3 file, the closer the array to the efficiency, the greater the INT Len = 0;while (len = Fis.read (by))!=-1) {fos.write (len); Fos.flush ();} Fos.close (); Fis.close ();}}

Java Learning Lesson 49th-io Flow (iii): Buffer 2 & Decorative design mode

Related Article

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.