装饰者模式——IO流运用

推荐博客
Java设计模式学习09
Java设计模式学习09
Java设计模式学习09

装饰者模式还是比较难懂的。。。。,我也说不了啥,详解看上面博客,下面案例是我仿照上面博客写的。
  • Behaver 定义行为接口

      public interface  Behavior {
           void dressing();//化妆
      }
    
  • Person 人类

    public class Person implements Behavior {
      private Behavior behavior;
      public Person(Behavior behavior) {
        this.behavior = behavior;
      }
    
      @Override
      public void dressing() {
         behavior.dressing();
      }
    }
    
  • Animal 不可否认人也是动物

      public class Animal implements Behavior {
          @Override
          public void dressing() {
              System.out.println("化妆。。。");
          }
      }
    
  • 男人

      public class Man extends Person {
    
          public Man(Behavior behavior) {
              super(behavior);
          }
    
          @Override
          public void dressing() {
              super.dressing();
              System.out.println("化男人妆。。。");
          }
      }
    
  • 女人

      public class Woman extends Person {
    
          public Woman(Behavior behavior) {
              super(behavior);
          }
    
          @Override
          public void dressing() {
              super.dressing();
              System.out.println("化女人妆。。。");
          }
      }
    
  • 测试类

      /**
       * 装饰者案例
       */
      public class Demo {
    
          public static void main(String[] args) {
              System.out.println("=====================动物化妆========================");
              Animal animal = new Animal();
              animal.dressing();
    
              System.out.println("=====================男人化妆========================");
              Man man = new Man(animal);
              man.dressing();
    
              System.out.println("=====================女人化妆========================");
              Woman woman = new Woman(animal);
              woman.dressing();
    
              System.out.println("=====================人妖化妆========================");
              Woman woman1 = new Woman(man);
              woman1.dressing();
          }
      }
    
结果
装饰者模式——IO流运用_第1张图片

我们可以看到,实例化女人1的时候换了一个实例组合,于是就出现了新的功能。。。

字符输出流使用缓冲----FileReader和BufferedReader

  • FileReader 文件字符输入流
  • BufferedReader 缓冲字符输入出流
BufferedReaderr缓冲字符输出流,直接继承自Reader

构造方法

public BufferedReader(Reader var1, int var2) {
        super(var1);
        this.markedChar = -1;
        this.readAheadLimit = 0;
        this.skipLF = false;
        this.markedSkipLF = false;
        if(var2 <= 0) {
            throw new IllegalArgumentException("Buffer size <= 0");
        } else {
            this.in = var1;
            this.cb = new char[var2];
            this.nextChar = this.nChars = 0;
        }
    }

    public BufferedReader(Reader var1) {
        this(var1, defaultCharBufferSize);
    }

同BufferWriter一样,两个构造方法。实例化的时候需要一个Reader实例。

运用案例
/**
 * 读取Demo6.java的内容
 */
public class Demo{
    public static final String PATH = "/Volumes/huang/studyfromGitHub/JavaForAndroid/JavaForAndroid/series10/src/main/java/com/example/Demo6.java";

    public static void main(String[] args) {
        //1.申明
        FileReader reader = null;
        BufferedReader bufferedReader = null;
        try {
            //2.实例化
            reader = new FileReader(PATH);
            bufferedReader = new BufferedReader(reader);
            //3.读取
            String msg=null;
            while ((msg=bufferedReader.readLine())!=null){//如果是null,则表示文件结束
                System.out.println(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
结果
装饰者模式——IO流运用_第2张图片

装饰者模式再运用----自定义BufferReader

  • 自定义BufferReader类

      class CoustomerBufferReader {
          private Reader in;
          //缓冲数组
          private char[] buffer=new char[1024];
          //每次读取的字符个数
          private int nChars;
          //缓冲数组的索引下标
          private int nextChar;
    
          public CoustomerBufferReader(Reader in) {
              this.in = in;
          }
    
          public int read() throws IOException {
              if (nChars == 0) {//表示缓冲中没有可读的字符
                  //读取节点流(字符流)中数据到缓冲数组中
                  nChars = in.read(buffer);
                  nextChar = 0;
              }
              if (nChars < 0) {//判断是否读取末尾
                  return -1;
              }
              int num = buffer[nextChar++];//从缓冲数组中获取一个字符
              nChars--;//读一轮,减一个
              return num;
          }
    
          public String readLine() throws IOException {
              int num = -1;
              StringBuilder builder = new StringBuilder();
              while ((num = read()) != -1) {
                  if (num == '\r') {
                      continue;
                  } else if (num == '\n') {
                      return builder.toString();
                  }
                  builder.append((char) num);//将读取的一个字符存放到变量中
              }
              //防止最后一行不存在换行符时,无法读取最后一行
              if (builder.length() > 0)
                  return builder.toString();
    
              return null;
          }
    
      }
    
  • 测试类

      public class Demo{
    
          public static void main(String[] args) {
              try {
                  //读取Demo6中的内容
                  FileReader reader = new FileReader(Demo7.PATH);
                  //使用自己的CoustomerBufferReader
                  CoustomerBufferReader bufferReader=new CoustomerBufferReader(reader);
                  String line=null;
                  while ((line=bufferReader.readLine())!=null){
                      System.out.println(line);
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
    
结果
装饰者模式——IO流运用_第3张图片

缓冲字符输入,输出流复制文件案例

讲道理正长是这样写

    public class Demo {
        //拷贝源文件
        public static final String PATH = "/Volumes/huang/studyfromGitHub/JavaForAndroid/JavaForAndroid/series10/src/main/java/com/example/Demo6.java";
        public static final String NAME = "testDemo9.java";

        public static void main(String[] args) {
            FileReader reader = null;
            BufferedReader bufferedReader = null;

            FileWriter fileWriter = null;
            BufferedWriter bufferedWriter = null;

            try {
                reader = new FileReader(PATH);
                bufferedReader = new BufferedReader(reader);

                fileWriter = new FileWriter(Config.PATH + NAME);
                bufferedWriter = new BufferedWriter(fileWriter);
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    //读一行,写一行
                    bufferedWriter.write(line);
                    bufferedWriter.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

运行一看结果,傻眼了:



它一行写到死。这可不行,我们看一下,除了没换行,其他都对,那我们控制它换行就可以了。

  • 完美版 核心代码替换

          boolean isFirstLine=true;//刚开始绝B第一行
          while ((line = bufferedReader.readLine()) != null) {  //读一行,写一行
             if (isFirstLine){//第一行直接写
                 bufferedWriter.write(line);
                  isFirstLine=false;//写完之后绝B不是第一行了
              }else {
                  bufferedWriter.newLine();//插入换行
                 bufferedWriter.write(line);
              }
                bufferedWriter.flush();
           }
    
  • 运行结果


    装饰者模式——IO流运用_第4张图片

你可能感兴趣的:(装饰者模式——IO流运用)