java_IO_Properties,SequenceInputStream,文件切割以及合并。
1 package day08; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.util.Properties; 9 /** 需求:将一个文件分割成大小相同的几部分碎片, 10 * 思路:切割文件时,必须记录住被切割文件的名称,以及切割出来碎片文件的个数。 以方便于合并。 11 * 这个信息为了进行描述,使用键值对的方式。用到了properties对象 12 * 13 * @param args 14 * @throws IOException 15 */ 16 public class SplitFileDemo { 17 18 private static final int SIZE = 1024*1024; 19 20 public static void main(String[] args) throws IOException { 21 File file = new File("d:\\DemoCopy\\286.jpg"); 22 spiteFile(file); 23 } 24 public static void spiteFile(File file) throws IOException{ 25 26 //输入流关联被分割的文件 27 FileInputStream fis = new FileInputStream(file); 28 29 //properties 30 Properties prop = new Properties(); 31 32 //得到文件路径 33 File dir = new File(file.getParent()); 34 //设置碎片文件目标路径 35 File des_dir = new File(dir+"\\partfile"); 36 des_dir.mkdir(); 37 38 //设置配置文件路径及名称 39 File properties_dir = new File(des_dir,file.getName()+".properties"); 40 41 //输出流 42 FileOutputStream fos = null; 43 byte[] buf = new byte[SIZE]; 44 int len =0; 45 int count=1; 46 47 //分割并且写入碎片操作 48 while((len=fis.read(buf))!=-1){ 49 fos = new FileOutputStream(new File(des_dir,count+".part")); 50 count++; 51 fos.write(buf, 0, len); 52 fos.close(); 53 } 54 fis.close(); 55 56 //写入配置文件操作 57 prop.setProperty("filename", file.getName()); 58 prop.setProperty("partcount", --count+""); 59 60 fos = new FileOutputStream(properties_dir); 61 prop.store(fos, "partfile info"); 62 63 } 64 65 }
1 package day08; 2 3 import Filter.SuffixFilter; 4 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.SequenceInputStream; 11 import java.sql.Connection; 12 import java.util.ArrayList; 13 import java.util.Collections; 14 import java.util.Enumeration; 15 import java.util.Properties; 16 /** 需求:把几个碎片文件合并成源文件。 17 * 思路:操作碎片文件,所以要用到序列流SequenceInputStream 18 * 合并成源文件过程中要知道碎片文件类型,文件数,要操作properties。期中文件类型要用到文件过滤器 19 * 20 * 21 * @param args 22 * @throws IOException 23 */ 24 public class MergeFileDemo { 25 26 27 public static void main(String[] args) throws IOException { 28 File dir = new File("D:\\DemoCopy\\partfile"); 29 mergeFile(dir); 30 } 31 public static void mergeFile(File dir) throws IOException{ 32 33 //获取指定目录下的配置文件对象。 34 File[] files = dir.listFiles(new SuffixFilter(".properties")); 35 36 if(files.length!=1) 37 throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一"); 38 //记录配置文件对象。 39 File confile = files[0]; 40 41 //获取该文件中的信息 42 Properties prop = new Properties(); 43 FileInputStream fis = new FileInputStream(confile); 44 prop.load(fis); 45 46 int count = Integer.parseInt(prop.getProperty("partcount")); 47 String name = prop.getProperty("filename"); 48 49 //获取所有碎片文件 50 File[] partfiles = dir.listFiles(new SuffixFilter(".part")); 51 if(partfiles.length!=count){ 52 throw new RuntimeException(" 碎片文件不符合要求,个数不对!应该"+count+"个"); 53 } 54 55 //将碎片文件和流对象关联 并存储到集合中。 56 ArrayList<FileInputStream> al = new ArrayList<FileInputStream>(); 57 for(int i=0; i<partfiles.length; i++ ){ 58 59 al.add(new FileInputStream(partfiles[i])); 60 61 } 62 63 //将多个流合并成一个序列流 64 Enumeration<FileInputStream> en= Collections.enumeration(al); 65 66 SequenceInputStream sis = new SequenceInputStream(en); 67 68 FileOutputStream fos = new FileOutputStream(new File(dir,name)); 69 int len = 0; 70 byte[] buf = new byte[1024]; 71 while((len=sis.read(buf))!=-1) 72 { 73 fos.write(buf,0,len); 74 } 75 76 fos.close(); 77 sis.close(); 78 79 } 80 }