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的文件。还有实现上传文件进度条功能。
这两个等实现了再补充。