为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式。

 

  装饰(decorator)你也可以翻译成修饰。比如:一个会精通化学数学的物理学家。在这个"物理学家"前面有两个修饰语分别是化学和数学。这两个修饰语就相当于把物理学家这个人让他又有了两项本领(功能)也就是会数学和化学。同样的装饰模式也就是不断给concrete Component增加修饰语,以扩展它自身的功能。

  装饰模式简介:

  Decorator Pattern的四大角色:
1.Component (抽象构件 一般是一个接口)【在io中就是Inputstream Outputstream Reader Writer】
2.ConcreteComponent(具体构件,要实现Component接口)【在io流中也就是Component的接口的子类减去Decoratoar本身及其子类】
3.Decorator(抽象装饰,一般是一个具体类,因为他有抽象的功能在里面,所有叫抽象装饰,要实现Component接口)[在io中就是FilterInputStream FilterOutputStream FilterReader ]不包括FilterWriter因为这个抽象类,没有任何子类。
4.ConcreteDecorator(具体装饰) 【在io中也就是上述所述Decorator的子类】
编程步骤:
1.写一个接口:Component
2.写一个类ConcreteComponent实现Component
3.写一个类Decorator实现Component
4.写一个ConcretDecorator实现Component
5.在实际中这个ConcreteDecorator一般最少写三个即Decorator1  Decorator2   Decorator3  他们之间是继承关系。
代码如下:
1.Component :

public interface Component {
void operation();
}
2.ConcreteComponent:

ublic class ConcreteComponent implements Component {
 
@Override
public void operation() {
// TODO Auto-generated method stub
 
}
 

}
3.Decorator:

 
public class Decorator implements Component {
public  Component component;
 
public Decorator(Component component) {
this.component = component;
}
public Decorator() {
// TODO Auto-generated constructor stub
}
@Override
public void operation() {
// TODO Auto-generated method stub
System.out.println("I can do everything");
}
 

}
4.ConcreteDecorator :Fish

public class Fish extends Decorator {
public Fish(){
 
}
public Fish(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("fish can swim in the water ");
}
 
}
5.ConcreteDecorator : Bird

public class Bird extends Fish {
public Bird(){
 
}
public Bird(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("virtue people like bird ,the can overview all the landscape ");
 
}
 
}
6.ConcreteDecorator Snipe

public class Snipe extends Bird {
public Snipe() {
// TODO Auto-generated constructor stub
}
public Snipe(Component component){
this.component=component;
}
@Override
public void operation() {
// TODO Auto-generated method stub
super.operation();
System.out.println("Snipe is very cuddly and shewd animal");
}
 
}

7.Test 测试类:

public class Test {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Fish fish=new Fish(new ConcreteComponent());
Bird bird=new Bird(fish);
new Snipe(bird).operation();
}
 

}

8.上述代码的输出结果为:

I can do everything
fish can swim in the water 
virtue people like bird ,the can overview all the landscape 
Snipe is very cuddly and shewd animal

而在io中设计者正式充分用了这个装饰模式对concreteComponent进行不断扩充的。

例如几个最常见的装饰:
E:\\Test.java表示一个路径
1.读取写入文件的装饰:BufferedReader in=newBufferedReader(new FileReader("E:\\Test.java"));  BufferedWriter out=new BufferedWriter(new FileWriter("E:\\Test.java"))
2.给读取写入的文件前加上编号:只需给1中的前面在加上一个修饰即:LineNumberReader即可。也就是:new LineNumberReader(in)和new LineNumberReader(out);
3.读取写入压缩文件的修饰:
  写一个压缩文件的修饰如下:
//write a file E:\\Ouyangfeng.gz

FileOutputStream f=new FileOutputStream("E:\\Ouyangfeng.gz");
CheckedOutputStream csum=new CheckedOutputStream(f,new Adler32());
ZipOutputStream zip=new ZipOutputStream(csum);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(zip));

读一个压缩文件的修饰如下:

//Read file ouyangfeng.gz
FileInputStream fi=new FileInputStream("E:\\ouyangfeng.gz");
CheckedInputStream csumi=new CheckedInputStream(fi, new Adler32());
ZipInputStream zis=new ZipInputStream(csumi);
BufferedReader in2=new BufferedReader(new InputStreamReader(zis));

.....

当然io流中的修饰远不止这些,但是我们只要知道这种装饰的思想就可以轻松的驾驭它。

下面我想把我写的压缩文件的程序分享给大家希望大家能掌握这种思想:

代码如下:

package com.qls.compression;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipCompressionTest5 {

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//把普通文本文件放入压缩文件中
FileOutputStream f=new FileOutputStream("E:\\ouyangfeng.gz");
CheckedOutputStream csum=new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos=new ZipOutputStream(csum);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(zos));
for(int i=1;i<=4;i++){
String str="E:\\Test"+i+".java";
zos.putNextEntry(new ZipEntry(str));
String s;
BufferedReader in=new BufferedReader(new FileReader(str));
while((s=in.readLine())!=null){
out.write(s+System.getProperty("line.separator"));
// out.newLine();//实现换行。

}
in.close();
out.flush();
}
out.close();
//Read file ouyangfeng.gz
FileInputStream fi=new FileInputStream("E:\\ouyangfeng.gz");
CheckedInputStream csumi=new CheckedInputStream(fi, new Adler32());
ZipInputStream zis=new ZipInputStream(csumi);
BufferedReader in2=new BufferedReader(new InputStreamReader(zis));
/*//第一种方法
ZipEntry ze;
while( (ze=zis.getNextEntry())!= null){
System.out.println("Reading file: "+ze);
String s;
while((s=in2.readLine())!=null){
System.out.println(s);
}
int x;
while((x=in2.read())!=-1){
System.out.print((char)x);
}
}*/
//第二种方法
ZipFile zf=new ZipFile("E:\\ouyangfeng.gz");
Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zf.entries();
while(e.hasMoreElements()){
ZipEntry ze = e.nextElement();
System.out.println("reading file:"+ze);
BufferedReader br=new BufferedReader(new FileReader(ze.toString()));
String s;
while((s=br.readLine())!=null){
System.out.println(s);
}

}
}

}

 

最新文章

  1. java模拟post请求发送json
  2. oracle xmltype导入并解析Excel数据 (二)规则说明
  3. 几个有用的jQuery代码片段
  4. 结对编程—黄金点游戏WinForm单机版
  5. ARP 实现
  6. .NET反射
  7. 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)
  8. js和jquery实现显示隐藏
  9. LOJ#2127「HAOI2015」按位或
  10. FF笔试题整理
  11. 基于docker的spark-hadoop分布式集群之一: 环境搭建
  12. ARM、X86/Atom、MIPS、PowerPC
  13. Android Studio 1.1.0 向导页(首页) 解析,以及版本控制 (SVN 和 GIT 的检出)
  14. openstack 部署(Q版)-----环境准备篇
  15. mysql与redis的区别与联系
  16. Eclipse_生成webservice客户端
  17. java的break跳出多层循环
  18. jquery.fullpage 全屏滚动
  19. 燕十八mysql笔记
  20. 解决SDK未授权问题

热门文章

  1. ORA-12705: Cannot access NLS data files or invalid
  2. JAVA大作业汇总3
  3. LeetCode:22. Generate Parentheses(Medium)
  4. 《数据结构与算法分析:C语言描述》读书笔记
  5. APPium-python实例(记录)
  6. 基于Python的接口自动化-01
  7. Laxcus大数据管理系统2.0(3)- 第一章 基础概述 1.2 产品特点
  8. python第三天(list,元组,dictionary)
  9. Failed loading D:\Program Files\phpStudy20161103\php\php-5.6.27-nts\ext\php_xdebug.dll
  10. linux备忘录-账号管理与ACL权限设定