package split;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.File;
public class Split {
public static void main(String[] args) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\test.pdf", "r");
int point = 0;//最开始
long last = 0;//最后
long lenth = randomAccessFile.length();//总长
int size = 5;//文件大小,单位:M;
int i = 0;
while (true) {
byte[] buffer = new byte[1024 * 1024];//缓冲区
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(
new File("C:\\Users\\Administrator\\Desktop\\zz\\test.part" + i++)
)
);
last = lenth - point;
if (last > buffer.length * size) {
for (int j = 0; j < size; j++) {
randomAccessFile.read(buffer);
bufferedOutputStream.write(buffer);
bufferedOutputStream.flush();//保存到磁盘上
}
} else {
byte[] endBuffer = new byte[(int) last];
randomAccessFile.read(endBuffer);
bufferedOutputStream.write(endBuffer);
bufferedOutputStream.close();
bufferedOutputStream.flush();
break;
}
point += buffer.length * size;
}
randomAccessFile.close();
//
// ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
// 10,11,1, TimeUnit.SECONDS,
// new ArrayBlockingQueue<>(10),
// new ThreadPoolExecutor.CallerRunsPolicy()
// //跑着的,排满了后正跑着的和新增的,做完没事干要销毁的,指定时间单位,排队的,叫回没排上队的
// );
// Thread thread = new Thread(){
// @Override
// public void run() {
//全文复制到这里使用多线程
// }
// };
// threadPoolExecutor.execute(thread);
// threadPoolExecutor.shutdown();
}
}
标签:文件,分割,java,int,randomAccessFile,bufferedOutputStream,import,new
From: https://www.cnblogs.com/used-conduit-onion/p/18537599