一、实现文件切割功能
01. import java.io.File;
02. import java.io.FileInputStream;
03. import java.io.FileOutputStream;
04. import java.io.IOException;
05. import java.util.Properties;
06.
07. public class SplitFileDemo{
08.
09. private static final int SIZE = 1024*1024;
10.
11. public static void main(String[] args) throws IOException{
12. File file = new File("0.mp3" );
13. splitFile(file);
14. }
15.
16. public static void splitFile(File file) throws IOException {
17. //用读取流关联源文件
18. FileInputStream fis = new FileInputStream(file);
19.
20. //定义一个1M的缓冲区
21. byte[] buf = new byte[SIZE];
22.
23. //创建目的
24. FileOutputStream fos = null;
25.
26. int len = 0;
27. int count = 1;
28.
29. //切割文件时,必须记录住被切割文件的名称,以及切割出来碎片文件的个数,以方
便于合并。
30. //这个信息为了进行描述,使用键值对的方式,用到了properties对象。
31.
32. Properties prop = new Properties();
33.
34. File dir = new File("c:\\partFiles" );
35. if(!dir.exists())
36. dir.mkdirs();
37.
38. while((len = fis.read(buf)) != -1){
39. fos = new FileOutputStream(new File(dir,(count++) +
".part"));
40. fos.write(buf,0,len);
41. fos.close();
42. }
43.
44. //将被切割文件的信息保存到prop集合中
45. prop.setProperty( "partcount",count + "" );
46. prop.setProperty( "filename",file.getName());
47.
48. fos = new FileOutputStream(new File(dir,count + ".properties" ));
49.
50. //将prop集合中的数据存储到文件中
51. prop.store(fos, "save file info");
52.
53. fis.close();
54. fos.close();
55. }
56. }
二 、实现文件合并功能
01. import java.io.File;
02. import java.io.FileInputStream;
03. import java.io.FileOutputStream;
04. import java.io.FilenameFilter;
05. import java.io.IOException;
06. import java.io.SequenceInputStream;
07. import java.util.ArrayList;
08. import java.util.Collections;
09. import java.util.Enumeration;
10. import java.util.Iterator;
11. import java.util.Properties;
12.
13. public class MergeFile{
14. public static void main(String[] args) throws IOException {
15. File dir = new File("c:\\partFiles" );
16. mergeFile(dir);
17. }
18.
19. public static void mergeFile(File dir) throws IOException {
20.
21. //获取指定目录下的配置文件对象
22. File[] files = dir.listFiles( new SuffixFilter(".properties" ));
23.
24. if(files.length!=1)
25. throw new RuntimeException(dir + ",该目录下没有properties扩
展名的文件或者不唯一" );
26.
27. //记录配置文件对象
28. File confile = files[0];
29.
30. //获取该文件中的信息
31. Properties prop = new Properties();
32. FileInputStream fis = new FileInputStream(confile);
33.
34. prop.load(fis);
35.
36. String filename = prop.getProperty( "filename");
37.
38. int count = Integer.parseInt(prop.getProperty("partcount"));
39.
40. //获取该目录下的所有碎片文件
41. File[] partFiles = dir.listFiles( new SuffixFilter(".part" ));
42.
43. if(partFiles.length != (count - 1)){
44. throw new RuntimeException("碎片文件不符合要求,个数不对!应该
是" + count + "个");
45. }
46.
47. //将碎片文件和流对象关联并存储到集合中
48. ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
49.
50. for(int x = 1; x <= partFiles.length; x++){
51. al.add( new FileInputStream(partFiles[x-1]));
52. }
53.
54. final Iterator<FileInputStream> it = al.iterator();
55.
56. //将多个流合并成一个序列流
57. Enumeration<FileInputStream> en = Collections.enumeration(al);
58.
59. SequenceInputStream sis = new SequenceInputStream(en);
60.
61. FileOutputStream fos = new FileOutputStream(new
File(dir,filename));
62.
63. byte[] buf = new byte[1024*1024];
64.
65. int len = 0;
66.
67. while((len = sis.read(buf)) != -1){
68. fos.write(buf,0,len);
69. }
70.
71. fos.close();
72. sis.close();
73. }
74. }
75.
76. class SuffixFilter implements FilenameFilter{
77. private String suffix;
78.
79. public SuffixFilter(String suffix){
80. super();
81. this.suffix = suffix;
82. }
83.
84. public boolean accept(File dir,String name){
85. return name.endsWith(suffix);
86. }
87. }