感觉这个应该是毕老师视频中关于流操作的比较复杂的一个了,涉及流,文件,集合,异常处理。- package com.itheima;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.io.SequenceInputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Enumeration;
- import java.util.Properties;
- import java.util.Set;
- /**
- * 文件的切割与合并
- * @author wfq
- */
- public class Test9 {
- public static void main(String[] args) {
- // File file = new File("tempfile\\那片海.mp3");
- File targfFile = new File("tempfile\\targfile");
- // split(file,targfFile);
- merger(targfFile);
- }
- //合并文件方法
- public static void merger(File targfFile) {
-
- //判断该文件是否存在且为目录
- if(!(targfFile.exists()&&targfFile.isDirectory()))
- throw new RuntimeException("该文件不存在或不是目录...");
-
- //判断配置文件是否存在且唯一
- File[] filesProp = targfFile.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".properties");
- }
- });
- if(!(filesProp.length==1))
- throw new RuntimeException("配置文件不存在或不唯一...");
- //创建属性集读取配置文件
- Properties prop = new Properties();
- BufferedInputStream propIn = null;
-
- String fileName = null;
- int fileCount = 0;
- try {
- propIn = new BufferedInputStream(new FileInputStream(filesProp[0]));
- prop.load(propIn);
- Set<String > propSet = prop.stringPropertyNames();
- for (String string : propSet) {
- fileName = string;
- fileCount = Integer.parseInt(prop.getProperty(fileName));
- }
- } catch (IOException e) {
- System.out.println("异常啦..");
- } finally {
- if(propIn!=null)
- try {
- propIn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //判断碎片文件是否缺失或不连续
- File[] filesPart = targfFile.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".part");
- }
- });
- //判断碎片数异常
- if(fileCount!=filesPart.length)
- throw new RuntimeException("碎片数异常...");
- //判断是否有碎片缺失
- for (int i = 1; i <= fileCount; i++) {
- File partFile = new File(targfFile, i+".part");
- if(!partFile.exists())
- throw new RuntimeException(partFile.getName()+"碎片缺失..");
- }
- //合并文件
- mergerFile(targfFile,filesPart,fileName);
- }
-
- private static void mergerFile(File targfFile, File[] filesPart,
- String fileName) {
- //创建存储流的集合
- ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
- //存入关联每一个碎片文件的流对象
- for (File file : filesPart) {
- try {
- list.add(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- //获取输入流的枚举
- Enumeration<FileInputStream> en = Collections.enumeration(list);
- //创建序列读取流
- SequenceInputStream in = new SequenceInputStream(en);
- //创建输出流
- BufferedOutputStream out = null;
-
- try {
- out = new BufferedOutputStream(new FileOutputStream(new File(targfFile, fileName)));
- byte[] buf = new byte[1024];
- int len = 0;
- while ((len=in.read(buf))!=-1) {
- out.write(buf, 0, len);
- out.flush();
- }
- } catch (IOException e) {
- System.out.println("异常啦..");
- } finally {
- //关闭资源
- if(in!=null)
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(out!=null)
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //切割文件方法
- public static void split(File file, File targfFile){
- //判断被切割的文件是否是是文件并存在
- if(!(file.isFile()&&file.exists()))
- throw new RuntimeException("被切割的文件不存在或不是文件...");
- //判断目标目录是否存在,不存在创建
- if(!targfFile.exists())
- targfFile.mkdir();
- //创建属性集,持久化存储配置文件
- Properties prop = new Properties();
- //创建以该大小为单位的切割文件的缓冲数组
- byte[] buf = new byte[1024*1024];
- //创建读写流
- BufferedInputStream bfi = null;
- BufferedOutputStream bfo = null;
- //记录生成文件编号
- int fileNum = 0;
- //完成切割
- try {
- bfi = new BufferedInputStream(new FileInputStream(file));
-
- int len = 0;
- while ((len=bfi.read(buf))!=-1) {
- bfo = new BufferedOutputStream(new FileOutputStream(new File(targfFile, (++fileNum)+".part")));
- bfo.write(buf, 0, len);
- bfo.flush();
- }
- //记录配置数据
- prop.setProperty(file.getName(), String.valueOf(fileNum));
-
- bfo = new BufferedOutputStream(new FileOutputStream(new File(targfFile, (++fileNum)+".properties")));
- //写出数据到配置文件
- prop.store(bfo, "wfq");
-
- } catch (IOException e) {
- System.out.println("出错啦...");
- } finally {
- //关闭资源
- if(bfi!=null)
- try {
- bfi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(bfo!=null)
- try {
- bfo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- }
复制代码
|