`

黑马程序员24-1:SuffixFilter扩展名过滤器,根据配置文件,文件切割/合并,SequenceInputStream 顺序流

 
阅读更多
------- android培训 java培训、期待与您交流!-------


package cn.itcast.io.p1.splitfile;

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;
import java.util.Properties;

public class MergeFile {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		File dir = new File("c:\\partfiles");
		
		mergeFile_2(dir);
	}
	//根据配置文件合并分割文件
	public static void mergeFile_2(File dir) throws IOException {
		
		/*
		 * 获取指定目录下的配置文件对象。
		 */
		//自定义扩展名过滤器,实现了FilenameFilter
		File[] files = dir.listFiles(new SuffixFilter(".properties"));
		
		if(files.length!=1)
			throw new RuntimeException(dir+",该目录下没有properties扩展名的文件或者不唯一");
		//记录配置文件对象。
		File confile = files[0];
		
		
		
		//获取该文件中的信息================================================。
		
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream(confile);
		
		prop.load(fis);
		
		String filename = prop.getProperty("filename");		
		int count = Integer.parseInt(prop.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 x=0; x<partFiles.length; x++){
			
			al.add(new FileInputStream(partFiles[x]));
		}
		
		
		
		//将多个流合并成一个序列流。 
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,filename));
		
		byte[] buf = new byte[1024];
		
		int len = 0;
		while((len=sis.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		
		fos.close();
		sis.close();

	}

	public static void mergeFile(File dir) throws IOException{
		
		
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		
		for(int x=1; x<=3 ;x++){
			al.add(new FileInputStream(new File(dir,x+".part")));
		}
		
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		SequenceInputStream  :x sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,"1.bmp"));
		
		byte[] buf = new byte[1024];
		
		int len = 0;
		while((len=sis.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		
		fos.close();
		sis.close();
		
	}

}


package cn.itcast.io.p1.splitfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*
 * 文件切割器。
 * 
 * 
 * 
 * 
 */

public class SplitFileDemo {

	private static final int SIZE = 1024 * 1024;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		File file = new File("c:\\aa.mp3");

		splitFile_2(file);
	}

	private static void splitFile_2(File file) throws IOException {

		// 用读取流关联源文件。
		FileInputStream fis = new FileInputStream(file);

		// 定义一个1M的缓冲区。
		byte[] buf = new byte[SIZE];

		// 创建目的。
		FileOutputStream fos = null;

		int len = 0;
		int count = 1;
		
		
		/*
		 * 切割文件时,必须记录住被切割文件的名称,以及切割出来碎片文件的个数。 以方便于合并。
		 * 这个信息为了进行描述,使用键值对的方式。用到了properties对象
		 * 
		 */
		Properties prop  = new Properties();

		File dir = new File("c:\\partfiles");
		if (!dir.exists())
			dir.mkdirs();

		while ((len = fis.read(buf)) != -1) {

			fos = new FileOutputStream(new File(dir, (count++) + ".part"));
			fos.write(buf, 0, len);
			fos.close();
		}
		
		//将被切割文件的信息保存到prop集合中。
		prop.setProperty("partcount", count+"");
		prop.setProperty("filename", file.getName());

		fos = new FileOutputStream(new File(dir,count+".properties"));
		
		//将prop集合中的数据存储到文件中。 
		prop.store(fos, "save file info");

		fos.close();
		fis.close();

	}

	public static void splitFile(File file) throws IOException {

		// 用读取流关联源文件。
		FileInputStream fis = new FileInputStream(file);

		// 定义一个1M的缓冲区。
		byte[] buf = new byte[SIZE];

		// 创建目的。
		FileOutputStream fos = null;

		int len = 0;
		int count = 1;

		File dir = new File("c:\\partfiles");
		if (!dir.exists())
			dir.mkdirs();

		while ((len = fis.read(buf)) != -1) {

			fos = new FileOutputStream(new File(dir, (count++) + ".part"));
			fos.write(buf, 0, len);
		}

		fos.close();
		fis.close();

	}

}


package cn.itcast.io.p1.splitfile;

import java.io.File;
import java.io.FilenameFilter;

public class SuffixFilter implements FilenameFilter {

	private String suffix;
	
	public SuffixFilter(String suffix) {
		super();
		this.suffix = suffix;
	}

	@Override
	public boolean accept(File dir, String name) {

		return name.endsWith(suffix);
	}

}


------- android培训 java培训、期待与您交流!-------


详细请查看:http://edu.csdn.net/heima -------

分享到:
评论

相关推荐

    IO体系.java

    将多个读取流合并成一个读取流,可以方便操作多个读取流。原理(迭代器)。 OutputStream |--FileOutputStream/:文件输出流。用于将写入File和FileDescriptor的输出流。用来操作图像,声音,视频等原始字节流。 ...

    JAVA IO流缓冲字节流缓冲字符流等流经典代码示例加注释总结.rar

    2、常用21个IO流:FileWriter、FileReader、CharArrayReader、CharArrayWriter、CharSequence、OutputStreamWriter、FileOutputStream、InputStreamReader、PrintWriter、BufferedReader、InputStream、...

    深刻理解java io

    5) SequenceInputStream:把多个InputStream合并为一个InputStream 2) Out stream 1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中 2) FileOutputStream:把信息存入文件中 3) PipedOutputStream:实现...

    java IO章节的总结

    4)SequenceInputStream:这个类可以将几个输入流串联在一起合并为一个输入流,构造函数有: SequenceInputStream(Enumeration e)枚举类型e中包含了若干个要被串联的输入流 SequenceInputStream(InputStream s1, ...

    Java之IO流学习总结

    File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。 9....

    教你彻底明白Java的IO系统

    1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型: 1.1 以字节为导向的stream 以字节为导向的stream,表示以...

    Java程序设计语言期末试题

    5) SequenceInputStream:把多个InputStream合并为一个InputStream 2) Out stream 1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中 2) FileOutputStream:把信息存入文件中 3) PipedOutputStream:实现...

    PCMAudioPlayerDemo:使用 AudioTrack 播放 PCM 音频文件

    PCMAudioPlayerDemothis project Using AudioTrack play PCM audio files##chinese这里要感谢...##说明该demo实现将多个pcm的音频文件合并到一个流中(合并主要用的是SequenceInputStream),通过用AudioTrack进行播放。

Global site tag (gtag.js) - Google Analytics