最近小弟我在自学Java,学到了IO流,没有深入学习,看到了有文件拆分和合并的练习题,我就练习了一下,不过我发现实际操作起来还是有些难度的。下面的拆分文件的代码花了我好几个小时,不断地出错,不断地改进,逻辑错误,代码错误都有,不过还好最后写了出来。写合并文件的代码时已经有了经验,其实就和拆分一个道理,很快就完成了,最后我打开拆分又合并的eclipse并成功运行的时候心情特别激动。虽然这不是什么成就,也没什么难度,但也带给了我很大的学习Java的兴趣和信心。
要拆分的是图第二个文件:
拆分后:
合并后:
能正常启动拆分合并后的eclipse.exe.
下面是拆分和合并的两段代码:
package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;;
//找到一个大于100k的文件,按照100k为单位,拆分成多个子文件,并且以编号作为文件名结束。
//比如文件 eclipse.exe,大小是408k。
//拆分之后,成为
//eclipse1.exe
//eclipse2.exe
//eclipse3.exe
//eclipse4.exe
//eclipse5.exe
public class IOFileWork1 {
public static void main(String[] args) {
byte[][]b=new byte[4][102400];
try {
File f=new File("d:/JavaGame/eclipse.exe");
FileInputStream fis=new FileInputStream(f);
byte[]bt=new byte[(int)f.length()];
fis.read(bt);//把文件内容读取到bt数组中
//拆分bt数组为五个byte类型的数组
int j=0;
int m=0;
for(int i=1;i<(int)f.length();i++) {
b[j][m]=bt[i-1];
m++;
if(i%102400==0) {j++;m=0;}
if(j==4)break;
}
byte[]b1=new byte[(int)f.length()-102400*4];//因为最后一个数组的长度和其他的不一样,所以不能一起初始化
for(int i=102400*4;i<(int)f.length();i++) {
b1[i-102400*4]=bt[i];
}
//建立路径
File f1 =new File("d:/JavaGame/新建文件夹/eclipse1.exe");
File f2 =new File("d:/JavaGame/新建文件夹/eclipse2.exe");
File f3 =new File("d:/JavaGame/新建文件夹/eclipse3.exe");
File f4 =new File("d:/JavaGame/新建文件夹/eclipse4.exe");
File f5 =new File("d:/JavaGame/新建文件夹/eclipse5.exe");
//建立输出流
FileOutputStream fos1=new FileOutputStream(f1);
FileOutputStream fos2=new FileOutputStream(f2);
FileOutputStream fos3=new FileOutputStream(f3);
FileOutputStream fos4=new FileOutputStream(f4);
FileOutputStream fos5=new FileOutputStream(f5);
//按照建立的路径写入文件
fos1.write(b[0]);
fos2.write(b[1]);
fos3.write(b[2]);
fos4.write(b[3]);
fos5.write(b1);
File ff=new File("d:/JavaGame/新建文件夹");
File[]fs=ff.listFiles();
for(int i=0;i<fs.length;i++) {
System.out.print(" "+fs[i]);
System.out.println("长度为:"+fs[i].length());}
//关闭输出流
fos1.close();
fos2.close();
fos3.close();
fos4.close();
fos5.close();
// //测试拆分的文件对不对
// System.out.println(bt[0]);
// System.out.println(bt[102400]);
// System.out.println(bt[204800]);
// System.out.println(bt[307200]);
// System.out.println(bt[409600]);
// System.out.println(b[0][0]);
// System.out.println(b[1][0]);
// System.out.println(b[2][0]);
// System.out.println(b[3][0]);
// System.out.println(b1[0]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}```
//把上述拆分出来的文件,合并成一个原文件。
//
//以是否能正常运行,验证合并是否正确
package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;;
public class IOFileWork2 {
public static void main(String[] args) {
//建立文件路径
File f1 =new File("d:/JavaGame/新建文件夹/eclipse1.exe");
File f2 =new File("d:/JavaGame/新建文件夹/eclipse2.exe");
File f3 =new File("d:/JavaGame/新建文件夹/eclipse3.exe");
File f4 =new File("d:/JavaGame/新建文件夹/eclipse4.exe");
File f5 =new File("d:/JavaGame/新建文件夹/eclipse5.exe");
//初始化用于暂存各个拆分文件内容的数组
byte[]bt=new byte[(int)f1.length()+(int)f2.length()+(int)f3.length()+(int)f4.length()+(int)f5.length()];
byte[]bt1=new byte[(int)f1.length()];
byte[]bt2=new byte[(int)f2.length()];
byte[]bt3=new byte[(int)f3.length()];
byte[]bt4=new byte[(int)f4.length()];
byte[]bt5=new byte[(int)f5.length()];
//建立输入流
try {
FileInputStream fis1=new FileInputStream(f1);
FileInputStream fis2=new FileInputStream(f2);
FileInputStream fis3=new FileInputStream(f3);
FileInputStream fis4=new FileInputStream(f4);
FileInputStream fis5=new FileInputStream(f5);
//将各个拆分文件的内容存储到字节数组中
fis1.read(bt1);
fis2.read(bt2);
fis3.read(bt3);
fis4.read(bt4);
fis5.read(bt5);
//将各个拆分文件的内容整合到一个字节数组中
int j=0;
for(int i=0;i<bt1.length;i++) {
bt[j]=bt1[i];
j++;
}
for(int i=0;i<bt2.length;i++) {
bt[j]=bt2[i];
j++;
}
for(int i=0;i<bt3.length;i++) {
bt[j]=bt3[i];
j++;
}
for(int i=0;i<bt4.length;i++) {
bt[j]=bt4[i];
j++;
}
for(int i=0;i<bt5.length;i++) {
bt[j]=bt5[i];
j++;
}
//将整合好的文件内容写入对应路径的文件
File ff=new File("d:/JavaGame/新建文件夹/eclipse.exe");
FileOutputStream fos=new FileOutputStream(ff);
fos.write(bt);
//关闭输入流和输出流
fos.close();
fis1.close();
fis2.close();
fis3.close();
fis4.close();
fis5.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}