Java serialization 100 character reading and writing with buffer, decorator mode

Keywords: Java MyEclipse github Big Data

1, BufferedWriter

1. Test with character reading and writing with buffer

 

package com.bjpowernode.java_learning;

import java.io.*;

​

public class D100_1_BufferedWriter {

  public static void main(String[] args) throws Exception{

    //Create character output stream with buffer

    String address = "C:\\\\Users\\\\lenovo1\\\\Workspaces\\\\MyEclipse CI\\\\Java_learning\\\\src\\\\com\\\\bjpowernode\\\\java_learning\\\\temp1.txt";

    BufferedWriter bw = new BufferedWriter(new FileWriter(address));

    //Or write like this

    //BufferedWriter bw = new BufferedWriter(OutputStreamWriter(new FileOutputStream(address,true)));

    //Start writing

    bw.write("jdfoishjfuji");

    //Write a line separator

    bw.newLine();

    bw.write("This is a test statement");

   

    //Refresh

    bw.flush();

    //Close

    bw.close();

   

    //Use BufferedReader and BufferedWriter To complete the copy

    String address2 = "C:\\\\Users\\\\lenovo1\\\\Workspaces\\\\MyEclipse CI\\\\Java_learning\\\\src\\\\com\\\\bjpowernode\\\\java_learning\\\\temp2.txt";

    BufferedReader br = new BufferedReader(new FileReader(address));

    BufferedWriter bw2 = new BufferedWriter(new FileWriter(address2));

    String temp = null;

    while((temp=br.readLine())!=null) {

      bw2.write(temp);

      bw2.newLine();

    }

    bw2.flush();

    br.close();

    bw2.close();

   

  }

​

}

2, Decorator mode

1. A simple example

For the m1 method in the A100 class, if we want to extend it, a simple method we can think of is to create a new class B100, and then override the method

But this kind of association is too strong, so you can use other methods -- decorator mode

2. Precautions

(1) Requirements in decorator mode: the decorator contains the reference of the decorator

(2) In the decorator pattern, the decorator and the decorated should realize the same type

 

package com.bjpowernode.java_learning;

​

public class D100_2_DecoratorMode {

  public static void main(String[] args) throws Exception{

    A100 a = new A100();

    B100 b = new B100();

    a.m1();

    b.m1();

    System.out.println("=============");

    //1.Create decorator

   

    FileReader fr = new FileReader();

    //2.Create decorator

    BufferedReader br = new BufferedReader(fr);

   

    //3.Indirectly execute the method in the decorated person by executing the method in the decorated person

    br.close();

  }

​

}

class A100{

  public void m1() {

    System.out.println("m1");

  }

}

class B100 extends A100{

  public void m1() {

    super.m1();

    System.out.println("Rewrote");

  }

}

//Use BufferedReader Yes FileReader Medium close Method to extend

class BufferedReader extends parent100{//BufferedReader100 Decorator

  //Correlation

  FileReader reader;//FileReader It's the decorator

  //Construction method

  BufferedReader(FileReader reader){

    this.reader = reader;

  }

  //Yes FileReader Medium close Method to extend

  public void close() {

    //extend

    System.out.println("Extension code 1");

    reader.close();

    System.out.println("Extension code 2");

  }

}

class FileReader extends parent100{

  public void close() {

    System.out.println("guanbi");

  }

}

abstract class parent100{

  public abstract void close();

}

3, Source code:

D100_1_BufferedWriter.java

D100_2_DecoratorMode.java

https://github.com/ruigege66/Java/blob/master/D100_1_BufferedWriter.java

https://github.com/ruigege66/Java/blob/master/D100_2_DecoratorMode.java

2.CSDN: https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruige0000/

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.

 

Posted by blckspder on Sat, 21 Mar 2020 08:48:50 -0700