面向对象的思想实现文件分割

随机访问文件(RandomAccessFile)里面有个seek方法可以随机指定访问的起始位置,在它的构造方法里面有读“r”,写“rw”模式

package cn.io.com;

import java.io.*;

public class TestRandStream {
    public static void main(String[] args) throws IOException {
     File file = new File("src/cn/io/com/TestBufferReader.java");
     //文件的大小
     long len = file.length();

     //一块的大小
     int blockSize = 15;

     //一共分多少块
        int size = (int)Math.ceil((len*1.0/blockSize));
        System.out.println(size);
        //起始位置
        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 = len-actualSize;
            }
            System.out.println(i+"-->"+beginPos+"-->"+actualSize);
            testRandom2(i,beginPos,actualSize);
        }
    }
    public static void testRandom()throws IOException {
        RandomAccessFile raf = new RandomAccessFile(new File("src/cn/io/com/TestBufferReader.java"),"r");
        raf.seek(5);
        byte[] bytes = new byte[1024];
        int tmp;
        while((tmp = raf.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,tmp));
        }
        raf.close();
    }
    public static void testRandom2(int i,int beginPos,int actualSize) throws IOException{
        //分块思想 起始位置 和实际大小
        RandomAccessFile raf = new RandomAccessFile(new File("src/cn/io/com/TestBufferReader.java"),"r");
//        //给定一个起始位置
//        int beginPos = 1030;
//        //要一个实际大小
//        int actualSize = 1025;
        raf.seek(beginPos);
        byte[] bytes = new byte[1024];
        int tmp;
        while((tmp = raf.read(bytes)) != -1) {
            if (tmp < actualSize) {
                System.out.println(new String(bytes,0,tmp));
                actualSize = actualSize-tmp;
            }else {
                System.out.println(new String(bytes,0,actualSize));
                break;
            }
        }
        raf.close();
    }
}

面向对象的思想实现文件分割

package cn.io.com;

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

public class TestRandStream2 {
    //源头
    private File srcFile;

    //目的地 是一个文件夹
    private String destDir;

    //所有分割后的文件存储路径
    private List<String> destPath;

    //每块大小
    private int blockSize;

    //块数:多少块
    private int size;

    public TestRandStream2(File srcFile,String destDir) {
        this(srcFile,destDir,1024);
    }

    public TestRandStream2(File srcFile,String destDir,int blockSize) {
        this.srcFile = srcFile;
        this.destDir = destDir;
        this.blockSize = blockSize;
        this.destPath = new ArrayList<>();
        init();
    }
    private void init() {
        //初始化
        //文件的大小
        long len = this.srcFile.length();
        
        //一共分多少块
        this.size = (int)Math.ceil((len*1.0/blockSize));

       //路径
        for (int i = 0; i < size; i++) {
            this.destPath.add(this.destDir+"/"+i+"-"+this.srcFile.getName());
        }
    }
    public void spilt() throws IOException {
        //分割,计算每一块的起始位置和大小
        //文件的大小
        long len = this.srcFile.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 = len-actualSize;
            }
            spiltDetail(i,beginPos,actualSize);
        }
    }
    private  void spiltDetail(int i, int beginPos, int actualSize) throws IOException {
        //分块思想 起始位置 和实际大小
        RandomAccessFile raf = new RandomAccessFile(this.srcFile,"r");
        RandomAccessFile raf2 = new RandomAccessFile(this.destPath.get(i),"rw");
//        //给定一个起始位置
//        int beginPos = 1030;
//        //要一个实际大小
//        int actualSize = 1025;
        raf.seek(beginPos);
        byte[] bytes = new byte[1024];
        int tmp;
        while((tmp = raf.read(bytes)) != -1) {
            if (tmp < actualSize) {
                //System.out.println(new String(bytes,0,tmp));
                raf2.write(bytes,0,tmp);

                actualSize = actualSize-tmp;
            }else {
                //System.out.println(new String(bytes,0,actualSize));
                raf2.write(bytes,0,actualSize);
                break;
            }
        }
        raf2.close();
        raf.close();
    }

    public static void main(String[] args) throws IOException {
        TestRandStream2 trs = new TestRandStream2(new File("src/cn/io/com/TestRandStream2.java"),"dest2");
        trs.spilt();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值