用到的知识:过滤器(FileNameFilter)

分割流:sequenceInputStream

配置流:Properties

package test;


import java.io.*;
import java.sql.SQLClientInfoException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.zip.InflaterInputStream;

import javax.annotation.processing.FilerException;
import javax.management.RuntimeErrorException;

import privateclass.Filterby_Name;
import privateclass.Filterby_hidden;
import privateclass.Filterby_java;
import privateclass.MyBufferedReader;

public class Main {

    private static final String space_operator = " ";
    private static final double pi = Math.PI;
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	private static final int SIZE = 1024*1024;
    public static void main(String[] args) throws Exception {


    	File file = new File("1.png");
    	File file2 = new File("d:\\图片分割");
    	cutting(file);
    	mergepart(file2);


    }
    /**
           *带有配置文件的存取文件(我们在配置文件里面存取分割了几个块和分割文件的名称)
     * @param file
     * @throws IOException
     */
	public static void cutting(File file) throws IOException {
		FileInputStream fis = new FileInputStream(file);
		byte[] buf = new byte[SIZE];

		int count = 1;
		int len = 0;
		FileOutputStream fos = null;
		File dirFile = new File("d:\\图片分割");
		if(!dirFile.exists())dirFile.mkdirs();
		while((len =  fis.read(buf)) != -1)
		{
			fos = new FileOutputStream(new File(dirFile,(count++) + ".part"));
			fos.write(buf,0,len);
			fos.close();
		}
		Properties prop = new Properties();
		prop.setProperty("filename", file.getName());
		prop.setProperty("count", count+"");
		fos = new FileOutputStream(new File(dirFile,count+".properties"));
		prop.store(fos, "filename + partcount");
		fos.close();
		fis.close();

	}

	public static void mergepart(File file2) throws IOException {
		/*
		 * 首先要拿到配置文件
		 * 使用过滤器
		 */
		File files[] = file2.listFiles(new Filterby_Name(".properties"));
		if(files.length != 1)
			throw new RuntimeException("主人,配置文件大于一个或没有配置文件,小可怜无能为力");
		Properties prop = new Properties();

		/*
		 * 获取配置文件里面的信息
		 */
		FileInputStream fis = new FileInputStream(files[0]);
		prop.load(fis);
		String name = prop.getProperty("filename");
		int count = Integer.valueOf(prop.getProperty("count"));
		File dirFiles[] = file2.listFiles(new Filterby_Name(".part"));

		/*
		 * 对照分割文件的数量
		 */

		if(dirFiles.length != count - 1)
			throw new RuntimeException("主人,文件数目对不上,小可怜无能为力");
		/*
		 * 将文件加入到list里面
		 */
		List<FileInputStream> list = new ArrayList<FileInputStream>();
		for(File file: dirFiles)
		{
			list.add(new FileInputStream(file));
		}

		Enumeration<FileInputStream> en = Collections.enumeration(list);

		/*
		 * 将文件放到一个合并流里面
		 */
		SequenceInputStream sis = new SequenceInputStream(en);

		FileOutputStream fos = new FileOutputStream(new File(file2,name));

		int len = 0;
		byte buf[] = new byte[1024];
 		while((len = sis.read(buf)) != -1)
 		{
 			fos.write(buf, 0, len);
 		}
 		sis.close();
 		fos.close();


	}
}




过滤器
package privateclass;

import java.io.File;
import java.io.FilenameFilter;
import java.util.jar.Attributes.Name;

public class Filterby_Name implements FilenameFilter {

	private String suffix;

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

	@Override
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		return name.endsWith(suffix);
	}

}

  

01-21 08:19