将文件切割
然后 把切割后的信息 写入配置文件
被切割文件的名字 路径 被切割的碎片个数
读取 配置信息 被切割文件的名字 路径 被切割的碎片个数 合并文件
public class splitFile { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String path = "E:/待切割文件.rmvb"; //将需要切割的文件封装成File对象 File f = new File(path); //split(f,path); //将被切割的文件合并 unionFile(); } public static void unionFile() throws Exception{ //首先读取配置信息 FileInputStream fis = new FileInputStream("E:/待切割文件.rmvb(split)/split.properties"); Properties pro = new Properties(); pro.load(fis); System.out.println(pro.getProperty("path")); String FileName = pro.getProperty("FileName"); String path = pro.getProperty("path"); int parts = Integer.parseInt(pro.getProperty("parts")); System.out.println(parts); File[] files = new File(path).listFiles(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith("part"); }}); if(files.length!=parts) System.out.println("文件碎片不足"); //创建合并文件 的存储文件夹 String unpath = path.replace("split","union"); File dir = new File(unpath); System.out.println(dir.mkdir()); //合并后的文件对象 File un = new File(unpath,FileName); //合并文件 集合 枚举 合并流 写入 ArrayList<FileInputStream> a = new ArrayList<FileInputStream>(); for (int i = 0; i < parts; i++) { fis = new FileInputStream(new File(path,(i+1)+".part")); System.out.println(i); a.add(fis); } final Iterator it = a.iterator();//变量it在局部位置的内部类被访问 所以it必须为final //Enumeration e = Collections.enumeration(a); Enumeration e = new Enumeration(){ @Override public boolean hasMoreElements() { // TODO Auto-generated method stub return it.hasNext(); } @Override public Object nextElement() { // TODO Auto-generated method stub return it.next(); }}; SequenceInputStream ss = new SequenceInputStream(e); FileOutputStream fos = new FileOutputStream(un); byte[] buf = new byte[1024*1024]; int len = 0; while ((len=ss.read(buf))!=-1) { fos.write(buf, 0, len); fos.flush(); } fos.close(); fis.close(); System.out.println("文件合并成功"); } public static void split(File f,String path) throws Exception{ String FileName = f.getName(); System.out.println("待切割文件名"+FileName); path +="(split)"; File dir = new File(path); System.out.println(dir.mkdir());//当文件不存在时才创建 File fp = new File(path,"1.part"); //关联 字节读取流 FileInputStream fis = new FileInputStream(f); FileOutputStream fos = new FileOutputStream(fp);//用于 写数据入 碎片的输出流 //定义字节缓冲数组 byte[] buf = new byte[1024*1024]; int len = 0; int part = 1;//用于 计数 作用于 碎片名 int count = 0;//用于计数 碎片是否已达到100M while ((len = fis.read(buf))!=-1) { count++; if (count==101) { fp = new File(path,(++part)+".part"); fos = new FileOutputStream(fp); count = 1; } fos.write(buf, 0, len);//没有else if执行完之后 继续执行 } System.out.println("文件切割完成。一共切割了"+part+"部分,碎片存的文件夹是:"+path); //将 切割的信息存放到 配置信息 split.properties中 Properties pro = new Properties(); // pro.put("FileName", FileName); // pro.put("parts", part); pro.setProperty("FileName", FileName); pro.setProperty("path", path); pro.setProperty("parts", String.valueOf(part)); //把poperties中的数据 store到输出流中 File poperties = new File(path,"split.properties"); fos = new FileOutputStream(poperties); pro.store(fos, null); fos.close(); } }