码迷,mamicode.com
首页 > 编程语言 > 详细

Java 文件分块及合并

时间:2016-08-27 11:11:21      阅读:598      评论:0      收藏:0      [点我收藏+]

标签:

利用Base64编码,再截字符串,仅支持小文件
小文件文件名随机,所以要将大文件信息和小文件顺序写入到小文件的第一行
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.6</version>
    </dependency>

 

import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Liwj on 2016/8/23.
 */
public class FileSplit {

    /**
     * 分割
     * @param fileName 文件路径
     * @param Number 分块文件个数
     * @throws Exception
     */
    public void splitByNumber(String fileName,int Number) throws Exception{
        File oldFile=new File(fileName);
        BufferedInputStream in=new BufferedInputStream(new FileInputStream(oldFile));
        String file=encode(in);
        int length=file.length();
        System.out.println("字符串长度:"+length);
        int size=length/Number;
        int start=0,end=size;
        BufferedOutputStream out=null;
        File newFile=null;
        String str_temp=null;
        for(int i=0;i<Number-1;i++){
            str_temp=i+" "+oldFile.getName()+"\n";
            str_temp+=file.substring(start,end);
            newFile=new File("E:\\result\\"+randNumber()+".file");
            out=new BufferedOutputStream(new FileOutputStream(newFile));
            out.write(str_temp.getBytes());
            out.close();
            start+=size;
            end+=size;
        }
        str_temp=Number-1+" "+oldFile.getName()+"\n";
        str_temp+=file.substring(start);
        newFile=new File("E:\\result\\"+randNumber()+".file");
        out=new BufferedOutputStream(new FileOutputStream(newFile));
        out.write(str_temp.getBytes());
        out.close();
        return;
    }

    /**
     * 文件合并
     * @param path
     * @throws Exception
     */
    public void mergeByName(String path) throws Exception{
        File file=new File(path);
        File list[]=file.listFiles();
        Map<String,String> map=new HashMap<String, String>();
        String newFileName=null;
        for(File f:list){
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            String str_head=reader.readLine();
            String id=str_head.substring(0,str_head.indexOf(" "));
            newFileName=str_head.substring(str_head.indexOf(" ")+1);
            map.put(id,f.getAbsolutePath());
            reader.close();
        }

        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<list.length;i++){
            File f=new File(map.get(String.valueOf(i)));
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            reader.readLine();
            String temp=null;
            while ((temp=reader.readLine())!=null){
                stringBuffer.append(temp);
            }
            reader.close();
        }
        BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("E:\\answer\\"+newFileName));
        out.write(decode(stringBuffer.toString()));
        out.close();
    }

    /**
     * 编码
     * @param in
     * @return
     * @throws IOException
     */
    public String encode(InputStream in) throws IOException{
        byte[] data = new byte[in.available()];
        in.read(data);
        return Base64.encodeBase64String(data);
    }

    /**
     * 解码
     * @param base64Str
     * @return
     * @throws IOException
     */
    public byte[] decode(String base64Str)throws IOException{
        return Base64.decodeBase64(base64Str);
    }

    /**
     * 随机数
     * @return
     */
    public String randNumber(){
        double number=Math.random();
        String str= String.valueOf(number);
        str=str.replace(".","");
        return str;
    }
    public static void main(String[] args){
        try {
            //分块
            //new FileSplit().splitByNumber("E:\\20160824134947.jpg",10);
            //合并
            //new FileSplit().mergeByName("E:\\result\\");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

Java 文件分块及合并

标签:

原文地址:http://www.cnblogs.com/zuferj115/p/5812461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!