package stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Splite {
public static void main(String[] args) throws IOException {
String fil = "D:/testFolder/SogouExplorer.exe";
int size = 102400;
// sp(fil, size);
co(fil, size);
}
/**
* 拆分文件
*
* @param fil绝对路径及文件名
* @param size拆分的大小
* @throws IOException
*/
public static void sp(String fil, int size) throws IOException {
File f = new File(fil);
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = null;
byte[] b = new byte[size];
long length = f.length();
for (int i = 0; length >= 0; i++) {
if (length < size) {
b = new byte[(int) length];
}
fis.read(b);
File f2 = new File(fil + "-" + i);
fos = new FileOutputStream(f2);
fos.write(b);
length -= size;
}
fis.close();
fos.close();
}
/**
* 合并文件
*
* @param fil
* @param size
* @throws IOException
*/
public static void co(String fil, int size) throws IOException {
File sub;
File file = new File(fil);
FileOutputStream fws = new FileOutputStream(file);
byte[] b = new byte[size];
for (int i = 0;; i++) {
sub = new File(fil + "-" + i);
if (!sub.exists()) {
break;
}
FileInputStream frs = new FileInputStream(sub);
if (sub.length() < size) {
b = new byte[(int) sub.length()];
}
while (frs.read(b) != -1) {
fws.write(b);
}
}
}
}
参考:https://blog.csdn.net/x183014563/article/details/88605157