javaSE中的输入输出流---一个读取流,对应多个输出流。而且生成的碎片文件都有有序的编号
2015-06-30 00:39
639 查看
<span style="font-size:18px;">package com.io.other.split; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class SplitFileTest { /** * 文件切割器。 * 一个读取流,对应多个输出流。而且生成的碎片文件都有有序的编号 * @param args * @throws IOException */ private static final int DEFAULT_SIZE=1024*2; public static void main(String[] args) throws IOException { File srcFile=new File("chen\\11.avi"); File destDir=new File("chen\\partfiles"); SplitFile(srcFile,destDir); } /** * * @param srcFile * @throws IOException */ public static void SplitFile(File srcFile,File destDir) throws IOException { if (!srcFile.exists()) { throw new RuntimeException(srcFile.getAbsolutePath()+",源文件不存在"); } if(!destDir.exists()){ destDir.mkdirs(); } //1、定义一个源文件 FileInputStream fis=new FileInputStream(srcFile); //2、创建目的引用 FileOutputStream fos=null; //3、创建一个缓冲区存储数据 byte[] buf=new byte[DEFAULT_SIZE]; int len=0; int count=0; while((len=fis.read(buf))!=-1){ //创建输出流,并明确要写入的文件 File partFile=new File(destDir,(++count)+".part"); fos=new FileOutputStream(partFile); fos.write(buf, 0, len); fos.close(); } //在产生碎片文件时,需要产生一个配置文件,记录碎片的个数和源文件名字 //partfile=n;filename=11.avi //配置文件中存储的键值信息。使用Properties集合 Properties prop=new Properties(); prop.setProperty("partcount", Integer.toString(count)); prop.setProperty("filename", srcFile.getName()); File conFile=new File(destDir,(++count)+".properties"); fos=new FileOutputStream(conFile); //关闭资源 fos.close(); fis.close(); } } </span>
相关文章推荐
- java-web 第五讲 应用程序的部署 和 会话跟踪
- struts2开发中在jsp页面顶用js取得action中传过来的值
- Eclipse 远程调试
- ssh+spring security+cas搭建(二)
- Java随机验证吗
- Java容器:容器一览
- 注解开发spring-aop 入门
- 2015062908 - EffactiveJava笔记 - 第46条 foreach优先传统for循环(4)
- Java enum的用法详解
- Eclipse快捷键
- [Java]学习笔记,随笔【六】
- [转载] Java NIO教程
- java实现——客户端登录
- 2015062907 - EffactiveJava笔记 - 第46条 foreach优先传统for循环(3)
- JAVA类图
- Java注解
- serverlet配置文件
- java 三层结构理解以及jdk环境配置
- Java 内部类
- spring整合xfire