5.26JavaIo文件分割_面向对象最终版

5.26JavaIo文件分割_面向对象最终版

定义外部属性

package iostudy.random;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
* 使用面向对象的思想封装好我们写好的文件分割的方法
* @since JDK 1.8
* @date 2021/5/28
* @author Lucifer
*/
public class SplitFile {
   /*定义属性*/
   //源文件
   private File src;
   //目的地(--->文件分割好以后的存储目录)
   private String destDir;
   //所有分割后的文件存储路径
   private List<String> destPath;
   //每块大小
   private int blockSize;
   //分割块数:多少块
   private int size;
}
/*
后期在外部直接调用构造器即可
把方法写在构造器里面
*/

定义构造器

    /**
    * 加入构造器初始化内容
    */
   public SplitFile(String srcPath, String destDir, int blockSize){
       this.src = new File(srcPath);
       this.destDir = destDir;
       this.blockSize = blockSize;
       /*初始化存储路径*/
       this.destPath = new ArrayList<String>();

       init();

       /*
       构造器可以重载,也可以重复调用
       封装--->写oop
        */
  }

定义初始化方法

    /**
    * 初始化方法
    */
   public void init(){
       /*初始化长度、大小、宽度*/
       /*总长度*/
       long len = this.src.length();
       /*块数--->注意小数,使用向上取整的方法*/
       this.size = (int) Math.ceil(len * 1.0 / blockSize); //转回int类型
       /*循环加入路径*/
       for (int i = 0; i < size; i++){
           this.destPath.add(this.destDir + "/" + i + "-" + this.src.getName());
      }
  }

定义分割方法

    /**
    * 使用分割方法
    * 1、计算每一块的起始位置及大小
    * 2、进行分割
    */
   public void split() throws IOException {
       /*总长度*/
       long len = src.length();

       /*起始位置和实际大小*/
       int beginPos = 0;
       int actualSize = (int) (blockSize > len ? len : blockSize);
       /*循环输出并且进行判断*/
       for (int i = 0; i < size; i++){
           /*指定大小*/
           beginPos = i * blockSize;
           //进行判断
           if (i==size - 1){
               //说明是最后一块
               actualSize = (int) len;
          }else {
               actualSize = blockSize;
               /*计算剩余量*/
               len -= actualSize;
          }
           splitDetail(i, beginPos, actualSize);
      }
  }

指定分割块的方法

    /**
    * 指定第i块的起始位置和实际长度
    * @param i             第几块
    * @param beginPos     起始位置
    * @param actualSize   实际长度
    * @throws IOException
    */
   private void splitDetail(int i, int beginPos, int actualSize) throws IOException {
       RandomAccessFile raf = new RandomAccessFile(this.src, "r"   );
       /*从第几个开始获取,注意定义的属性含义以及方法形参*/
       RandomAccessFile rafDest = new RandomAccessFile(this.destPath.get(i), "rw");
       /*随机读取*/
       raf.seek(beginPos);
       /*进行文件操作*/
       byte[] flush = new byte[1024]; //缓冲容器
       int len = -1; //接收的长度
       /*循环获取内容*/
       while ((len=raf.read(flush))!=-1){
           if (actualSize > len){
               //获取本次读取的所有内容
               rafDest.write(flush, 0, len); //长度所读取的全部长度
               actualSize -= size;
          }else {
               rafDest.write(flush, 0, actualSize); //长度是实际的长度
               break;
          }
      }
       /*关闭流*/
       rafDest.close();
       raf.close();
  }

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/14823650.html