IO_装饰设计模式_文件分割与合并

装饰设计模式

package com.bjsxt.io.pattern;

public class Voice {
	private int voice =10;
	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);
	}
	
}

package com.bjsxt.io.pattern;
/**
 * 扩音器
 * 类与类之间的关系
 * 1、依赖:形参|局部变量
 * 2、关联:属性
 * 		聚合:属性 整体与部分 不一致的生命周期 人与手
 *      组合:属性 整体与部分 一致的生命周期  人与大脑
 * 3、继承:父子类关系
 * 4、实现: 接口与实现类关系
 * 
 * 
 * 
 * 
 * @author Administrator
 *
 */
public class Amplifier {
	private Voice voice;
	public Amplifier() {
	}
	public Amplifier(Voice voice) {
		super();
		this.voice = voice;
	}
	
	public void say(){
		System.out.println(voice.getVoice()*1000);
	}
}

package com.bjsxt.io.pattern;

public class App {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Voice v =new Voice();
		v.say();
		Amplifier am =new Amplifier(v);
		am.say();
		
	}

}

文件分割与合并_RandomAccessFile
在这里插入图片描述
此类的实例支持对随机访问文件的读取和写入。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.bjsxt.io.others;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import com.bjsxt.io.util.FileUtil;
/**
 * 文件的分割思路
 * 1、分割的块数 size   n块
 * 2、每一块的大小 blockSize
 *   最后:总的文件大小 -(n-1)*blockSize
 * 
 * @author Administrator
 *
 */
public class RndDemo01 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		RandomAccessFile rnd =new RandomAccessFile(new File("E:/xp/20130502/test/test.java"),"r");
		rnd.seek(40);
		//定义缓冲大小
		byte[] flush =new byte[1024];
		//接收长度
		int len =0; 		
		
		while(-1!=(len=rnd.read(flush))){
			if(len>=20){
				System.out.println(new String(flush,0,20));
				break;
			}else{
				System.out.println(new String(flush,0,len));
			}
			
		}
		
		
		FileUtil.close(rnd);
		
	}

}

文件分割与合并_初始化各项参数__分割__文件合并_SequenceInputStream

package com.bjsxt.io.others;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.bjsxt.io.util.FileUtil;
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,1024);		
	}
	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;  //src为输入文件
		//健壮性
		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));  //Math.ceil(x) --返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入。Math.floor(x)--返回小于等于数字参数的最大整数,对数字进行下舍入
		//确定输出的每一块的文件名,及输出路径
		initPathName();
	}
	
	private void initPathName(){
		for(int i=0;i<size;i++){
			this.blockPath.add(destBlockPath+"/"+this.fileName+".part"+i);
		}
	}
	
	/**
	 * 文件的分割
	 * 0)、第几块
	 * 1、起始位置
	 * 2、实际大小
	 * @param destPath 分割文件存放目录
	 */
	public void split(){	
		long beginPos =0;  //起始点
		long actualBlockSize =blockSize; //实际大小		
		//计算所有块的大小、位置、索引
		for(int i=0;i<size;i++){
			if(i==size-1){ //最后一块
				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[1024];
			//接收长度
			int len =0;
			while(-1!=(len=raf.read(flush))){				
				if(actualBlockSize-len>=0){ //查看是否足够
					//写出
					bos.write(flush, 0, len);
					actualBlockSize-=len; //剩余量
				}else{ //写出最后一次的剩余量
					bos.write(flush, 0, (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 = 0; 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());			
				
			//缓冲区
			byte[] flush = new byte[1024];
			//接收长度
			int len =0;
			while(-1!=(len=sis.read(flush))){						
				bos.write(flush, 0, 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 = 0; i < this.blockPath.size(); i++) {
				bis = new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i))));
				
				//缓冲区
				byte[] flush = new byte[1024];
				//接收长度
				int len =0;
				while(-1!=(len=bis.read(flush))){						
					bos.write(flush, 0, len);
				}
				bos.flush();
				FileUtil.close(bis);
			}
		} catch (Exception e) {
		}finally{
			FileUtil.close(bos);
		}		
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SplitFile split = new SplitFile("E:/xp/20130502/test/学员设置(20130502).xls","E:/xp/20130502",51);
		
		//System.out.println(split.size);
		
		//split.split();
		
		split.merge("E:/xp/20130502/test1.xls");
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值