标签:style blog io color os ar 使用 java for
文件切割器
private static final int SIZE = 1024 *1024; public static void splitFile(File file) throws IOException{ //用读取流关联文件(不确定文件格式) FileInputStream fis = new FileInputStream(file);//源是一个 byte[] by = new byte[SIZE];//定义1M的缓冲区 FileOutputStream fos = null;//汇不知道有多少个 int len = 0; int count = 1;//记录子文件个数 File dir = new File("D:\\patFiles"); if(!dir.isFile()){ dir.mkdirs(); } while((len = fis.read(by))!=-1){ fos = new FileOutputStream(new File(dir,(count++)+".part"));//自定义文件格式 fos.write(by,0,len); } fos.close(); fis.close(); }
文件合并
public static void main(String[] args) throws IOException { File file = new File("D:\\PartFile"); Merge(file); } public static void Merge(File dir)throws IOException{ ArrayList<FileInputStream> AL = new ArrayList<FileInputStream>(); for(int i = 1;i<=7;i++){ AL.add(new FileInputStream(new File(dir,i+".part"))); } Enumeration<FileInputStream> en = Collections.enumeration(AL); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream(new File(dir,"盛夏光年.mp3")); byte[] by = new byte[1024]; int len = 0; while((len = sis.read(by))!=-1){ fos.write(by, 0, len); } sis.close(); fos.close(); }
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.Properties; public class Main { private static final int SIZE = 1024 *1024; public static void main(String[] args) throws IOException { File file1 = new File("d:\\NeedSplit\\盛夏光年.mp3"); File file2 = new File("D:\\PartFiles"); splitFile(file1); Merge_1(file2); } public static void splitFile(File file) throws IOException{ //用读取流关联文件(不确定文件格式) FileInputStream fis = new FileInputStream(file);//源是一个 byte[] by = new byte[SIZE];//定义1M的缓冲区 FileOutputStream fos = null;//汇不知道有多少个 int len = 0; int count = 1;//记录子文件个数 /*切割文件必须要记录切割文件的名称和切割处理的碎片文件的个数,方便合并 * 这个信息为了进行描述,使用键值对的方法,所以使用Properties对象*/ Properties pro = new Properties(); File dir = new File("D:\\PartFiles"); if(!dir.isFile()){ dir.mkdirs(); } while((len = fis.read(by))!=-1){ fos = new FileOutputStream(new File(dir,(count++)+".part"));//自定义文件格式 fos.write(by,0,len); fos.close(); } //将切割后文件的信息保存在pro集合中 pro.setProperty("partCount", count+""); pro.setProperty("fileName", file.getName()); fos = new FileOutputStream(new File(dir,count+".properties")); //将pro集合的信息存储在集合中 pro.store(fos, "save file infmation"); fis.close(); } public static void Merge_1(File dir)throws IOException{ //获取指定目录下配置文件对象 File[] files = dir.listFiles(new SuffixFilter(".properties"));//new一个过滤器 if(files.length!=1){ throw new RuntimeException(dir+"该目录下没有properties扩展名的文件或者不唯一 "); } //记录配置文件对象 File confile = files[0]; //获取配置文件信息 Properties pro = new Properties(); FileInputStream fis = new FileInputStream(confile);//关联流对象 pro.load(fis);//加载信息 String filename = pro.getProperty("fileName");//得到文件名 int count = Integer.parseInt(pro.getProperty("partCount"));//得到碎片个数 //获取该目录下的所有碎片文件 //定义过滤器,判断碎片文件的个数与配置信息中的碎片信息是否一致 File[] partFiles = dir.listFiles(new SuffixFilter(".part")); if(partFiles.length!=(count-1)){ throw new RuntimeException("碎片文件个数不对,应是"+count+"个!"); } //将碎片文件和流对象关联,并存储集合中 ArrayList<FileInputStream> AL = new ArrayList<FileInputStream>(); for(int i = 0;i<partFiles.length;i++){ AL.add(new FileInputStream(partFiles[i])); } //将多个流合并成一个序列流 Enumeration<FileInputStream> en = Collections.enumeration(AL); SequenceInputStream sis = new SequenceInputStream(en); //读写过程 FileOutputStream fos = new FileOutputStream(new File(dir,filename)); byte[] by = new byte[1024]; int len = 0; while((len = sis.read(by))!=-1){ fos.write(by, 0, len); } sis.close(); fos.close(); } }
标签:style blog io color os ar 使用 java for
原文地址:http://blog.csdn.net/wjw0130/article/details/40512119