多线程读写大文件,为大文件上传做准备

2022-02-08
RandomAccessFile简单认识,它允许我们在文件的任意位置插入字节,下面在一个新文件的第5和10位置插入字符

public static void main(String[] args) throws IOException {
        File wFile = new File("D:\\DATA\\randomAccessFileTest\\target.txt");
        RandomAccessFile rw = new RandomAccessFile(wFile, "rw");
        //在坐标为10的位置写入test
        rw.seek(10);
        rw.write("test".getBytes());
        //在坐标为5的位置写入hello
        rw.seek(5);
        rw.write("hello".getBytes());
        rw.close();
    }

结果:字节码文件如下,第5和第10个坐标开始插入内容,注意坐标从0开始,字节码是以16进制存储,字符对应关系可以查ASCII码对照表
在这里插入图片描述
在这里插入图片描述
同样的在第5个位置插入java

 public static void main(String[] args) throws IOException {
            File wFile = new File("D:\\DATA\\randomAccessFileTest\\target.txt");
            RandomAccessFile rw = new RandomAccessFile(wFile, "rw");
            //在坐标为5的位置写入hello
            rw.seek(5);
            rw.write("java".getBytes());
            rw.close();
        }

结果:
在这里插入图片描述
可以看得出,如果原有位置有内容会被替换,这也是避免说插入字节后,后面的字节被往后推,使得文件不正确。因为在文件分块上传的时候,并不是一定按顺序接收到字节的,有可能先插入后面的,那么插入后它的位置就不应该收到后面插入的字节的影响。
2021-09-28
原理:
比如有个1000字节的文件,使用5个线程进行拷贝,那么每个线程处理200个字节。第一个线程从0坐标读取200个字节。第二个线程从200坐标读取200个字节,以此类推。然后第一个从0坐标写入200个字节,第二个线程从200坐标写入200个字节,以此类推。

使用RandomAccessFile可以帮我们实现这个功能。
代码如下:

package com.yang.main;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 随机读写文件测试
 */
public class RandomAccessFileTest {
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(5,5,1, TimeUnit.MINUTES,new LinkedBlockingQueue<>(100));
    public static void main(String[] args) throws IOException {
        File rFile = new File("D:\\DATA\\randomAccessFileTest\\毕业设计.mp4");
        File wFile = new File("D:\\DATA\\randomAccessFileTest\\target.mp4");
        long size = rFile.length();
        long blockSize = size/executor.getCorePoolSize();
        long remainingSize = size%executor.getCorePoolSize();
        for (int i = 0; i < 5; i++) {
            int j = i;
            executor.execute(()->{
                try {
                    //记录起始位置
                    long index = j*blockSize;
                    //下一个位置
                    long nextIndex =(j+1)*blockSize;
                    //每次读取大小
                    byte[] bytes = new byte[2048];
                    RandomAccessFile randRFile = new RandomAccessFile(rFile, "r");
                    RandomAccessFile randWFile = new RandomAccessFile(wFile, "rw");
                    do {
                        randRFile.seek(index);
                        randRFile.read(bytes,0,index+bytes.length>nextIndex?(int)(nextIndex-index):bytes.length);
                        System.out.println("index___"+index+"___offset___"+randRFile.getFilePointer());
                        randWFile.seek(index);
                        randWFile.write(bytes,0,index+bytes.length>nextIndex?(int)(nextIndex-index):bytes.length);
                    } while ((index+=bytes.length)<nextIndex);
                    randWFile.close();
                    randRFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        if (remainingSize>0){
            //余数部分直接通过主线程处理
            int index = (int)(executor.getCorePoolSize()*blockSize);
            RandomAccessFile randRFile = new RandomAccessFile(rFile, "r");
            RandomAccessFile randWFile = new RandomAccessFile(wFile, "rw");
            byte[] buff = new byte[(int)remainingSize];
            randRFile.seek(index);
            randRFile.read(buff);
            randWFile.seek(index);
            randWFile.write(buff);
            randWFile.close();
            randRFile.close();
        }
    }
}

成功实现了图片和视频的拷贝
在这里插入图片描述
不过最主要的目的还是实现大文件上传。比如前端上传50G的文件。还有实现上传文件进度条功能。
这两个等实现了再补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值