IO流--SequenceInputStream序列化流的应用--文件合并
2013-12-10 22:25
239 查看
package cn.itheima.cway.IO; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; public class MergerFile { /**需求:合并文件 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // 指定待合并文件的目录 File file=new File("F:\\ss"); mergerFile(file); } /** * 功能:将part文件合并为一个文件 * 思路: * 1、需要读取part临时文件中的内容,所以,需要集合:ArrayList * 2、既然有读取,则需要文件读取流:FileInputStream * 3、既然是需要合并文件,则需要SequenceInputsteam * 4、最后需要把集合中的内容输出到一个文件,则需要输出流:FileOutputStream * @param dec * @throws IOException */ public static void mergerFile(File dec) throws IOException { //创建一个集合,用于存放读取到的part文件内容 ArrayList<FileInputStream> al=new ArrayList<FileInputStream>(); //将part文件读取到集合中 for(int i=1;i<=4;i++) { al.add(new FileInputStream(new File(dec,i+".part"))); } //合并后的文件名: File dir=new File(dec,"B.mp3"); if(!dir.exists()) dir.createNewFile(); //把集合中的元素转换为枚举 Enumeration<FileInputStream> en=Collections.enumeration(al); //创建序列化输入流,去读取part文件(碎片文件)的内容 SequenceInputStream sis=new SequenceInputStream(en); //创建一个输出流,用于保存合并后的文件内容 FileOutputStream fos=new FileOutputStream(dir); int len=0; byte[] buf=new byte[1024]; while((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } }
相关文章推荐
- javaday53_IO流_文件分割与文件合并的综合应用
- IO流 应用: 文件的 切割与合并
- 黑马程序员————IO流简单应用:分隔文件、合并文件
- 24-IO流-53-IO流(文件合并)
- IO流--SequenceInputStream切割(合并)文件--优化(Properties集合相结合)
- Java---IO流之序列流(文件合并)
- JAVA高级应用之IO流与文件的处理
- IO流_合并流读取多个文件的内容复制到一个文件中
- IO流--SequenceInputStream序列化流的应用---文件切割
- 黑马程序员------IO流 操作字节数组的流对象,对象序列化,打印流,随机存储文件,合并流,管道流,操作基本数据类型的流对象
- awk应用:文件合并
- 【java】IO流之切割与合并文件
- 24-IO流-54-IO流(文件切割合并+配置文件)
- Java中使用IO流实现大文件的分裂与合并
- Java的IO流文件的合并拆分和删除
- IO流加强之序列流---文件合并
- Git fetch pull 的应用, git 多人同时修改一个文件后的合并
- IO流(SequenceInputStream序列流--文件拆分与合并)
- JAVA基础 day22 File类的方法和应用 利用递归获取目录下的所有文件 Properties类的应用(和IO技术结合,存取配置文件) 打印流和合并流等功能类的学习应用 切割文件练习
- java基础的综合应用(类的应用,文本拷贝,IO流,数组,集合,文件文件夹的创建)