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步。创建源,选择流,操作,
如图
操作:递归打印,文件拷贝,关闭流的方法,文件的分割与合并。

最新文章

  1. 修改form表单的黄色背景
  2. android源码中修改wifi热点默认始终开启
  3. java环境变量设定
  4. .NET 4.5 WPF Ribbon
  5. 《Python CookBook2》 第一章 文本 - 改变多行文本字符串的缩进 &amp;&amp; 扩展和压缩制表符(此节内容待定)
  6. myeclipse报错:Could not create the view: An unexpected exception was thrown.
  7. 转:二十一、详细解析Java中抽象类和接口的区别
  8. java学习面向对象之this
  9. 解决Item控件抢占焦点
  10. Jquery 网站保存信息提示消息实现,提示后自动消失
  11. css1-颜色和长度
  12. .net ToString()用法详解与格式说明
  13. 1.1 Java概述上
  14. 配置你的Editor
  15. android-xBuild apk差分与合成,zip差分与合成,lua打包,apk打包,png/jpg图片压缩
  16. OwinHost.exe用法
  17. python select模块详解
  18. mybatis的配置和使用
  19. 天兔(Lepus)数据库监控系统安装笔记
  20. git配置SSH Key,上传本地代码至github

热门文章

  1. 第五章 HID设备
  2. 161. One Edit Distance
  3. Sass占位符选择器`%`
  4. SpeeDO —— 并行深度学习系统
  5. NTP 服务器配置
  6. poj3140Contestants Division
  7. css3动画(transition)属性探讨
  8. [LOJ 1008] Fibsieve`s Fantabulous Birthday
  9. [OpenJudge] 平方和
  10. [HDU 1203] I NEED A OFFER!