Java 分解大文件及合并文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//分解文件
public class DatagramPacket1 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\Users\86176\Desktop\s.jpg");//读一个文件
byte[] buf = new byte[1024 *10];//一次读10kb
int len;
int index1 = 0;
FileOutputStream fos = new FileOutputStream("C:\Users\86176\Desktop\wx\sk0"+ ++index1);//输出一个文件
int index2 = -1;
while ((len = fis.read(buf)) > 0){
++index2;
if (index2 % 5 == 0 && index2 != 0){
fos.flush();
fos.close();
fos = new FileOutputStream("C:\Users\86176\Desktop\wx\sk0"+ ++index1);
}
fos.write(buf,0,len);
}
fos.flush();
fos.close();
}
}






import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//合并文件
public class DatagramPacket2 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("C:/Users/86176/Desktop/wx/h.jpg");
for (int i = 1 ; i <= 3; i++){
FileInputStream fis = new FileInputStream("C:/Users/86176/Desktop/wx/sk0" + i);
fos.write(fis.readAllBytes());
}
fos.close();
}
}
原文地址:https://www.cnblogs.com/liuyunche/p/13849168.html