- 假设有一个大小为10G的视频文件(文件大小和类型不限定),然后将该文件分割成几个小一些的片段,并分别保存成一个个小的文件。
- . 把上面被分割的若干小文件,合并起来,恢复成原来的文件。 编写一个工具程序,实现上述功能(合并时注意小文件的顺序)
package com.Work4;
import org.junit.Test;
import java.io.*;
public class Four {
@Test
public void test01(){
reads(new File("E:\\WeChat.mp4"),"G:\\WeChat");
try {
for (int i = 0; i < 5; i++) {
merge(new File("G:\\WeChat" +"\\" + "文件"+ i + ".mp4"),
new File("G:\\合并后视频.mp4"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void reads(File file,String name){
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file);
byte [] bytes = new byte[1024*1024];
int len;
for (int i = 0; i < 5; i++) {
output = new FileOutputStream(new File(name,"文件" + i + ".mp4"));
int temp = 0;
while((len = input.read(bytes)) != -1){
output.write(bytes,0,len);
temp++;
output.write(bytes,0,len);
if(i != 4 && temp >= 2){
break;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void merge(File file,File newFile) throws IOException {
InputStream input = null;
OutputStream output = null;
input = new FileInputStream(file);
byte [] bytes = new byte[1024*1024];
output = new FileOutputStream(newFile,true);
int len;
while((len = input.read(bytes)) != -1){
output.write(bytes,0,len);
}
}
}