IO:文件分割与合并:附代码
package ff.learn.iow09;
import java.io.*;
public class homework03 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\丿维美\\Desktop\\设计阶段课设报告.doc");
division(file);
}
public static void division(File file) throws IOException {
if (file != null && file.exists()) {
File file1 = new File("C:\\Users\\丿维美\\Desktop\\problam\\temp1");
if (file1 == null || !file1.exists()) {
file1.mkdir();
}
long size = file.length();
long f = 1024 * 1024;
long t = size % f == 0 ? size / f : size / f + 1;
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int o = 0;
int num = 0;
for (int i = 0; i < t; i++) {
FileOutputStream out = new FileOutputStream("C:\\Users\\丿维美\\Desktop\\problam\\temp1\\" + (i + 1) + ".temp1");
while ((o = in.read(b)) != -1) {
out.write(b, 0, o);
num += o;
if (num >= f) {
num = 0;
break;
}
}
}
}
}
public static void join(File file) throws IOException {
if(file!=null&&file.exists()){
File[] f = file.listFiles();
int num = 0;
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\丿维美\\Desktop\\problam\\设计阶段课设报告.doc");
for (int i = 0; i < f.length; i++) {
num++;
FileInputStream in = new FileInputStream("C:\\Users\\丿维美\\Desktop\\problam\\temp\\"+num+".temp");
byte[] v = new byte[1024];
int o = 0;
while ((o=in.read(v))!=-1){
fileOutputStream.write(v,0,o);
}
in.close();
}
fileOutputStream.close();
}
}
}