Java的简单理解(25)---(随机访问文件)RandomAccessFile

Java

RandomAccessFile

/**
 * 文件的分割
 * 1. 分割的块数     n块
 * 2. 每一块的大小   blocksize
 * 3. 最后:总的文件大小 - (n - 1) * blocksize
 */
public void test(){

    try {
        File file = new File("E:/xp/test/a.txt");
        RandomAccessFile rnd = new RandomAccessFile(file,"r");

        rnd.seek(10);
        byte[] car = new byte[1024];
        int len = 0;

        while ((len = rnd.read(car)) != -1) {
            if (len >= 200) {
                System.out.println(new String(car,0,120));
            } else {
                System.out.println(new String(car,0,len));
            }
        }

        rnd.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

文件分割的思路

  • 第一步:分割的准备
    • 块数
    • 确定每块大小
    • 每块的名称
  • 第二步
    • 分割
    • 第几块,每块的起点,实际大小
    • 文件分割
public class SplitFile {

    // 文件的路径
    private String filePath;

    // 文件名
    private String fileName;

    // 文件大小
    private long length;

    // 块数
    private int size;

    // 每块的大小
    private long blockSize;

    // 每块的名称
    private List<String> blockPath;

    public SplitFile() {
        blockPath = new ArrayList<>();
    }

    public SplitFile(String filePath) {
        this(filePath,1024);
    }

    public SplitFile(String filePath,long blockSize){
        this();
        this.filePath = filePath;
        this.blockSize = blockSize;
        init();
    }

    /**
     * 初始化操作,计算块数,确定文件名
     */
    public void init() {
        File src = null;
        if (filePath == null || !((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);

    }

    /**
     * 确定文件名
     */
    public void initPathName(String destPath) {
        for (int i = 0; i < size; i++){
            this.blockPath.add(destPath + "/" + this.fileName + ".part" + i);
        }
    }

    /**
     * 文件的分割
     * @param destPath 分割文件存放目录
     */
    public void split(String destPath){

        // 确定文件的路径
        initPathName(destPath);

        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 = beginPos + actualBlockSize;
        }
    }

    /**
     * 文件的分割 输入 输出
     * 文件的拷贝
     * @param idx 第几块
     * @param beginPos 起始点
     * @param actualBlockSize 实际大小
     */
    public void spiltDetail(int idx,long beginPos,long actualBlockSize){
        // 1. 创建源
        File src = new File(this.filePath);
        // 2. 目标文件
        File dest = new File(this.blockPath.get(idx));
        // 3. 选择流
        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 = actualBlockSize - len;
                } else {
                    bos.write(flush,0, (int) actualBlockSize);
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

合并文件的两种方法
1.
public void mergeFile1(String destPath) {
    // 创建源
    File dest = new File(destPath);
    // 选择流
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(dest,true)); // true: 追加而不是替换
        for (int i = 0; i < this.blockPath.size(); i++) {
            bis = new BufferedInputStream(new FileInputStream(new File(blockPath.get(i))));

            // 缓冲区
            byte[] flush = new byte[1024];
            // 接收长度
            int len = 0;
            while ((len = bis.read(flush)) != -1) {
                bos.write(flush,0,len);
            }

            bos.flush();

            bis.close();
            bos.close();

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
2.
public void mergeFile2(String destPath) {
    // 创建源
    File file = new File(destPath);

    // 选择流
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    SequenceInputStream sis = null;

    // 创建一个容器
    Vector<InputStream> vector = new Vector<>();

    for (int i = 0; i < this.blockPath.size(); i++){
        try {
            vector.add(new BufferedInputStream(new FileInputStream(new File(blockPath.get(i)))));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    try {
        bos = new BufferedOutputStream(new FileOutputStream(file,true));
        sis = new SequenceInputStream(vector.elements());

        byte[] flush = new byte[1024];
        int len = 0;

        while ((len = sis.read(flush)) != -1) {
            bos.write(flush,0,len);
        }

        bos.flush();

        bos.close();
        bis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 石家庄:10.15当日所有结算均超时。10.16修改所有结算单状态,进行手动结算时,发现多结算了一笔 银行核心反馈...
    and天神阅读 331评论 0 0
  • 深入理解闭包: 一、变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域。 变量的作用域无非...
    老头子_d0ec阅读 269评论 0 0
  • Book1:创建你的未来 [if !supportLists]1.[endif]效仿成功者 每到一个新的领域开始工...
    糊糊_87阅读 480评论 0 0
  • 宇宙人生万物万象,归纳起来就是六十四种情境,它是完整的系统。无论哪一种情境,对于我们来讲都是一个大的环境,而且在每...
    两木相撑阅读 206评论 0 2
  • xilingyan阅读 132评论 0 0