JAVA learning lesson 49th-IO stream (III): Buffer Zone 2 & amp; Decoration Design Mode

Source: Internet
Author: User

JAVA learning lesson 49th-IO stream (III): Buffer Zone 2 & amp; Decoration Design Mode

I. Simulate BufferedReader

Custom MyBuffereaReader

In the buffer zone, an array is encapsulated to provide external methods for Array Operations. These methods ultimately operate on the array badge.

Principle: extract data from the source and store it in the buffer zone. After the data is obtained, the system continues to fetch data from the source until the data in the source is obtained, use-1 as the end mark

Import java. io. *; class MyBufferedReader {private FileReader fr; // defines the array as the buffer private char [] ch = new char [1024]; // defines a pointer for Array Operations, when the last element of the array is operated, the pointer returns to private int pos = 0; // defines a counter. When the data in the buffer is reduced to 0, add data from the source to the buffer. private int count = 0; MyBufferedReader (FileReader fr) {this. fr = fr;} public int Myread () throws IOException {// obtain a batch of data from the source to the buffer, first determine whether the count is 0if (count = 0) {count = fr. read (ch); pos = 0;} if (count <0) return- 1; char temp = ch [pos ++]; count --; return temp;} public String MyReadLine () throws IOException {// store the read data to 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 ();}}


Ii. Decoration Design Model

When the functions of a group of objects are enhanced, this mode can be used to solve the problem. The above custom MyBufferedReader class is a good embodiment. It is easier for FileReader to compare the original functions, of course, the above Code is relatively simple to write

Class Room {public void show () {System. out. println ("House") ;}} class NewRoom {private Room or; public NewRoom (OldRoom or) {// TODO Auto-generated constructor stubthis. or = or;} public void show () {or. show (); System. out. println ("Bungalow ");}}

You must avoid modifying the source code. Of course, inheritance can also enhance functions.

However, inheritance will make the inheritance system complex and troublesome.


Differences between decorative design patterns and inheritance:

Inheritance: If you want to enhance the function, you must continue to write sub-classes. just to create a new NewRoom, you must create a sub-class. After the NewRoom appears, you must create a sub-class gradually, as a result, the inheritance system becomes increasingly bloated.

Decoration Design Mode: no matter the NewRoom or Villa, it is a type of Room and maintains the basic attributes of the house.

Therefore, to use the decoration design mode, you only need to pass in the decorated object. You can extract and encapsulate the buffer technology separately. You only need to associate the buffer with the buffer, the relative system of such design will become simpler.


3. LineNumberReader

API documentation explanation: trace the buffer character input stream of the row number. This class defines the MethodsetLineNumber(int)AndgetLineNumber()They can be used to set and obtain the current row number respectively.

Import java. io. *; public class Main {public static void main (String [] args) throws IOException {FileReader fr = new FileReader ("ACM.txt"); LineNumberReader lr = new LineNumberReader (fr ); string str = null; lr. setLineNumber (10); // set the row number starting from 10 while (str = lr. readLine ())! = Null) {System. out. println (str + "line:" + lr. getLineNumber ();} lr. close ();}}

There are not many such use cases, so you can know how to use them.


Iv. Exercises

Make sure to use byte stream files to copy media files

Import java. io. *; public class Main {public static void main (String [] args) throws IOException {// copy_mp3_1 (); // copy_mp3_2 (); // This copy_mp3_3 ();} public static void copy_mp3_3 () throws IOException {// TODO Auto-generated method stubFileInputStream FD = new FileInputStream ("D: \ .mp3 "); FileOutputStream fos = new FileOutputStream (" D: \ 3.3.mp3 "); byte [] by = new byte [FCM. available ()]; // limitation: the file is too large That won't work. read (by); fos. write (by); fos. close (); FCM. close ();} public static void copy_mp3_2 () throws IOException {// TODO Auto-generated method stubFileInputStream FD = new FileInputStream ("D :\\ .mp3 "); bufferedInputStream br = new BufferedInputStream (FCM); FileOutputStream fos = new FileOutputStream ("D: \ 2.2.mp3"); bytes 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 FD = new FileInputStream ("D :\\ .mp3 "); fileOutputStream fos = new FileOutputStream ("D: \ 1.1.mp3"); byte [] by = new byte [1024]; // pay attention to the number of bytes of Mp3 files, the closer the array is to the efficiency, the more int len = 0; while (len = Fi. read ())! =-1) {fos. write (len); fos. flush () ;}fos. close (); FCM. close ();}}

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.