IO装饰设计模式:(IO中使用了装饰设计模式)
节点流可以直接从源读取数据,处理流就是对节点流的包装,这就是装饰,装饰就是对原有的流的性能的提升。比如买的车,马力不够,就进行装饰,使其马力增大。
装饰模式:
扩音器对声音进行了扩大。 类与类之间的关系(6种): 、依赖:一个对象是形参或者局部变量,只有调用方法的时候才会依赖这个类。
、关联:一个对象是属性。关联分为:
聚合:是属性 整体与部分关系, 不一致的生命周期, 人与手
组合:是属性 整体与部分关系, 一致的生命周期, 人与大脑
、继承:父子类关系。
、实现: 接口与实现类关系 public class Voice {
private int voice =;
public Voice() {
// TODO Auto-generated constructor stub
}
public int getVoice() {
return voice;
}
public void setVoice(int voice) {
this.voice = voice;
} public void say(){
System.out.println(voice);
} } /**
* 扩音器
* 类与类之间的关系:
* 1、依赖:一个对象是形参或者局部变量,只有调用方法的时候才会依赖这个类。
* 2、关联:一个对象是属性。关联分为:
* 聚合:是属性 整体与部分关系, 不一致的生命周期, 人与手
* 组合:是属性 整体与部分关系, 一致的生命周期, 人与大脑
* 3、继承:父子类关系。
* 4、实现: 接口与实现类关系。
*/
public class Amplifier {
private Voice voice;
public Amplifier() {
}
public Amplifier(Voice voice) {
super();
this.voice = voice;
}
public void say(){
System.out.println(voice.getVoice()*);
}
} public class App {
public static void main(String[] args) {
Voice v =new Voice();
v.say();
Amplifier am =new Amplifier(v);
am.say();
}
} .不能父目录拷贝到子目录中。
if(dest.getAbsolutePath().contains(src.getAbsolutePath())){
System.out.println("父目录不能拷贝到子目录中");
return;
} /**
* 文件的分割思路
* 1、分割的块数 size n块
* 2、每一块的大小 blockSize
* 最后:总的文件大小 -(n-1)*blockSize
*/
public class RndDemo01 {
public static void main(String[] args) throws IOException {
RandomAccessFile rnd =new RandomAccessFile(new File("E:/xp/test/a.txt"),"r");
rnd.seek();//跳过30字节再开始。一个数字是4个字节
//定义缓冲大小
byte[] flush =new byte[];
//接收长度
int len =;
while(-!=(len=rnd.read(flush))){
if(len>=){
System.out.println(new String(flush,,));
break;
}else{
System.out.println(new String(flush,,len));
}
}
FileUtil.close(rnd);
}
} 文件的分割与合并: public class SplitFile {
//原始文件的路径
private String filePath;
//原始文件名
private String fileName;
//原始文件大小
private long length;
//根据每块的大小,确定分多少块
private int size;
//每块的大小
private long blockSize;
//分割后的存放目录
private String destBlockPath;
//每块的名称
private List<String> blockPath; public SplitFile(){
blockPath = new ArrayList<String>();
}
public SplitFile(String filePath,String destBlockPath){
this(filePath,destBlockPath,);
}
public SplitFile(String filePath,String destBlockPath,long blockSize){//方法里面一个个调用另一个方法,这是面向过程的思路。
this();
this.filePath= filePath;
this.destBlockPath =destBlockPath;
this.blockSize=blockSize;
init();
} /**
* 初始化操作 计算 块数、确定文件名
*/
public void init(){
File src =null;
//健壮性
if(null==filePath ||!(((src=new File(filePath)).exists()))){
return;
}
if(src.isDirectory()){
return ;
}
//文件名
this.fileName =src.getName(); //计算块数 实际大小 与每块大小
this.length = src.length();
//修正 每块大小
if(this.blockSize>length){
this.blockSize =length;
}
//确定块数
size= (int)(Math.ceil(length*1.0/this.blockSize));
//确定文件的路径
initPathName();
} private void initPathName(){
for(int i=;i<size;i++){
this.blockPath.add(destBlockPath+"/"+this.fileName+".part"+i);
}
} /**
* 文件的分割
* 0)、第几块
* 1、起始位置
* 2、实际大小
* @param destPath 分割文件存放目录
*/
public void split(){
long beginPos =; //起始点
long actualBlockSize =blockSize; //实际大小
//计算所有块的大小、位置、索引
for(int i=;i<size;i++){
if(i==size-){ //最后一块
actualBlockSize =this.length-beginPos;
}
spiltDetail(i,beginPos,actualBlockSize);
beginPos+=actualBlockSize; //本次的终点,下一次的起点
}
}
/**
* 文件的分割 输入 输出
* 文件拷贝
* @param idx 第几块
* @param beginPos 起始点
* @param actualBlockSize 实际大小
*/
private void spiltDetail(int idx,long beginPos,long actualBlockSize){
//1、创建源
File src = new File(this.filePath); //源文件
File dest = new File(this.blockPath.get(idx)); //目标文件
//2、选择流
RandomAccessFile raf = null; //输入流,随机访问流。
BufferedOutputStream bos=null; //输出流
try {
raf=new RandomAccessFile(src,"r");
bos =new BufferedOutputStream(new FileOutputStream(dest));
//读取文件,定位到起始位置。
raf.seek(beginPos);
//缓冲区
byte[] flush = new byte[];
//接收长度
int len =;
while(-!=(len=raf.read(flush))){
if(actualBlockSize-len>=){ //查看是否足够
//写出
bos.write(flush, , len);
actualBlockSize-=len; //剩余量
}else{ //写出最后一次的剩余量
bos.write(flush, , (int)actualBlockSize);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
FileUtil.close(bos,raf);
}
}
/**
* 文件的合并
*/
public void merge(String destPath){
//创建源
File dest =new File(destPath);
//选择流
BufferedOutputStream bos=null; //输出流
SequenceInputStream sis =null ;//输入流
//创建一个容器
Vector<InputStream> vi = new Vector<InputStream>();
try {
for (int i = ; i < this.blockPath.size(); i++) {
vi.add(new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i)))));//把每块文件的流搞成一个集合。
}
bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
sis=new SequenceInputStream(vi.elements()); //java.io.SequenceInputStream.SequenceInputStream(Enumeration<? extends InputStream> e)
//public Enumeration<E> elements() //缓冲区
byte[] flush = new byte[];
//接收长度
int len =;
while(-!=(len=sis.read(flush))){//读到程序里面来,所以是输入流。
bos.write(flush, , len);//输出到目的地文件,所以是输出流。
}
bos.flush();
FileUtil.close(sis);
} catch (Exception e) {
}finally{
FileUtil.close(bos);
} }
/**
* 文件的合并
*/
public void merge1(String destPath){
//创建源
File dest =new File(destPath);
//选择流
BufferedOutputStream bos=null; //输出流
try {
bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
BufferedInputStream bis = null;
for (int i = ; i < this.blockPath.size(); i++) {
bis = new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i))));
//缓冲区
byte[] flush = new byte[];
//接收长度
int len =;
while(-!=(len=bis.read(flush))){
bos.write(flush, , len);
}
bos.flush();
FileUtil.close(bis);
}
} catch (Exception e) {
}finally{
FileUtil.close(bos);
}
} public static void main(String[] args) {
SplitFile split = new SplitFile("E:/xp/20130502/test/学员设置(20130502).xls","E:/xp/20130502",);
System.out.println(split.size);
split.split();
split.merge("E:/xp/20130502/test1.xls");
}
} IO总结:
IO操作步骤:4步。创建源,选择流,操作,
如图
操作:递归打印,文件拷贝,关闭流的方法,文件的分割与合并。

java15 IO装饰设计模式的更多相关文章

  1. java学习之IO装饰设计模式

    装饰设计模式就是对已有的对象的功能进行增强 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么自定义的该类称为装饰类. 装饰类通常会通过构造方法接收被装 ...

  2. java_设计模式_装饰设计模式

    package IO; /* * 装饰设计模式 模拟咖啡 * 1.抽象组件:需要装饰的抽象对象(接口或抽象父类) * 2.具体组件:需要装饰的对象 * 3.抽像装饰类:包含了对抽象组件的引用以及装饰着 ...

  3. 根据IO流源码深入理解装饰设计模式使用

    一:摘要 通过对java的IO类中我们可以得出:IO源码中使用装饰设计模式频率非常高, 对装饰设计模式而言,他能够避免继承体系的臃肿,同时也可以动态的给一个对象添加一些额外的功能,如果要扩展一个功能, ...

  4. Java中IO流中的装饰设计模式(BufferReader的原理)

    本文粗略的介绍下JavaIO的整体框架,重在解释BufferReader/BufferWriter的演变过程和原理(对应的设计模式) 一.JavaIO的简介 流按操作数据分为两种:字节流与字符流. 流 ...

  5. Java IO 流 -- 设计模式:装饰设计模式

    在java IO 流中我们经常看到这样的写法: ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream(ne ...

  6. [javaSE] IO流(装饰设计模式)

    装饰设计模式:当想要对已有的对象进行功能增强时,可以自定义类将已有的对象传入,并提供加强功能,自定义的该类称为装饰类 典型的: Reader--FileReader --BufferedReader ...

  7. java 28 - 3 设计模式之 装饰设计模式

    装饰设计模式 装饰设计模式概述 装饰模式就是使用被装饰类的一个子类的实例,在客户端将这个子类的实例交给装饰类.是继承的替代方案 优点 使用装饰模式,可以提供比继承更灵活的扩展对象的功能,它可以动态的添 ...

  8. Java基础---Java---IO流-----BufferedReader、BufferedWriter、缓冲区、装饰设计模式及和继承的区别

    IO流 IO流用来处理设备之间的数据传输 java对数据的操作是过流的方式 流按操作数据分为两种:字节流与字符流 流按流向分为:输入流,输出流. IO流常用基类 字节流的抽象基类:InputStrea ...

  9. JAVA之旅(二十六)——装饰设计模式,继承和装饰的区别,LineNumberReader,自定义LineNumberReader,字节流读取操作,I/O复制图片

    JAVA之旅(二十六)--装饰设计模式,继承和装饰的区别,LineNumberReader,自定义LineNumberReader,字节流读取操作,I/O复制图片 一.装饰设计模式 其实我们自定义re ...

随机推荐

  1. [topcoder]TallPeople

    水题.http://community.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854 一开始错了是因为理解错题意.还有就是 ...

  2. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  3. ServletInvocableHandlerMethod:167 - Error resolving argument

    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(R ...

  4. gtest的安装和测试[good]

    一.前言 本篇将介绍一些gtest的基本使用,包括下载,安装,编译,建立我们第一个测试Demo工程,以及编写一个最简单的测试案例. 二.下载 如果不记得网址, 直接在google里搜gtest,第一个 ...

  5. 【DataStructure In Python】Python模拟二叉树

    使用Python模拟二叉树的基本操作,感觉写起来很别扭.最近做编译的优化,觉得拓扑排序这种东西比较强多.近期刷ACM,发现STL不会用实在太伤了.决定花点儿时间学习一下STL.Boost其实也很强大. ...

  6. Form - CHECKBOX全选功能

    FORM BUILDER开发,遇到这样一个需求: 添加一个CHECKBOX完成全选功能,红框为新添加的CHECKBOX(如图示) Try to use APP_RECORD.FOR_ALL_RECOR ...

  7. scaleform mobile sdk for android 多点触摸 修正

    修正 scaleform 的多点触控 (随手一记 给后来的人做个参考) scaleform 版本号 4.2.24 (估计这就是最后一个 移动版的版本了,万年没有更新了) 开始 一直以为 scalefo ...

  8. bzoj1143 2718

    最小可相交路径覆盖 先预处理可到达的点然后转化为最小不相交路径覆盖 type node=record        point,next:longint;      end; ..] of node; ...

  9. Struts 2.3.1.1 命令执行漏洞

    漏洞版本: Struts 2.3.1.1 漏洞描述: CVE ID:CVE-2011-3923 Struts2的核心使用的是WebWork框架,而WebWork通过XWork来处理用户的请求参数.Xw ...

  10. MFC CVIew关闭时崩溃

    记得看视频的时候老师说过    创建CView的时候,也就是创建视图的时候,不要使用  Cview      m_view;这种方式 而是使用Cview  *  pView=new   Cview() ...