Java大文件拆片合并

文件拆片:主要需要注意的地方是记录读取每片文件的起始位置以及最后一片文件的大小

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

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

public class SplitFileTest {

    public static void main(String[] args) {
        SplitFileTest test=new SplitFileTest();
        test.slicePiecesFile(new File("E:/data/a.jpg"));
        //test.slicePiecesFile(new File("E:/data/b.txt"));
        List<File> fileList=new ArrayList<File>();
        fileList.add(new File("E:/data/1_a.jpg"));
        fileList.add(new File("E:/data/2_a.jpg"));
        fileList.add(new File("E:/data/3_a.jpg"));
        fileList.add(new File("E:/data/4_a.jpg"));
        fileList.add(new File("E:/data/5_a.jpg"));
        fileList.add(new File("E:/data/6_a.jpg"));
        test.mergeFile(fileList,"E:/data/aa.jpg");
        /*List<File> fileList=new ArrayList<File>();
        fileList.add(new File("E:/data/1_b.txt"));
        test.mergeFile(fileList,"E:/data/bb.txt");*/
    }
    public void  slicePiecesFile(File fileName)
    {
        Base64 base=new Base64();
        long uploadTime=System.currentTimeMillis()/1000;
        int fileIndex=1; //文件分片下表
        long perPiece = 1024*60;//默认每个分片文件的大小
        long start=0,end = 0;
        long fileSize=0;//文件大小
        String fileNameTmp=null;
        if (fileName.exists() && fileName.isFile()) {
            fileSize=fileName.length();
            fileNameTmp=fileName.getName();
            System.out.println("文件"+fileName+"的大小是:"+fileName.length());
        }
        //计算需要拆分的次数
        int num=(int) (fileSize/perPiece);
        if(fileSize%perPiece != 0)
            num++;
        //分片上传文件信息
        System.out.println("创建随机读写文件");
        RandomAccessFile randomAccessFile=null;
        FileOutputStream fos=null;
        try {
            randomAccessFile=new RandomAccessFile(fileName,"r");
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        System.out.println("进入拆分文件");
        byte[] buff = null;
        //循环读取文件信息
        for(int i=1;i<=num;i++)
        {
            
            if(i== num && fileSize%perPiece != 0)
                buff = new byte[(int) (fileSize%perPiece)];
            else
                buff = new byte[(int)perPiece];    
            try 
            {
                randomAccessFile.seek(start);//从指定位置开始读取
                randomAccessFile.read(buff);
                fos=new FileOutputStream("E:\\data\\"+i+"_"+fileNameTmp);
                fos.write(buff);
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            start+=buff.length;
            System.out.println(start);
        }
        System.out.println("拆分文件结束");
        
    }
    
    
    public void mergeFile(List<File> fileList,String FileName)
    {
        FileOutputStream fos=null;
        FileInputStream fis=null;
        System.out.println("合并文件开始");
        try 
        {
            fos=new FileOutputStream(new File(FileName));
            byte[] buff=new byte[1024];
            int length=0;
            for(File file:fileList)
            {
                fis=new FileInputStream(file);
                while((length=fis.read(buff)) != -1)
                {
                    fos.write(buff,0,length);
                    fos.flush();
                }
                
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        finally
        {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        System.out.println("合并文件结束");
        
    }

}
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过IO流来实现大文件的拆分与合并。 首先,对于大文件的拆分,可以通过RandomAccessFile类来实现,该类提供了seek()和read()方法,可以指定读取文件的位置和读取的字节数。可以根据需求将大文件拆分成多个小文件,每个小文件的大小相同或者不同。 对于大文件合并,可以通过FileInputStream和FileOutputStream类来实现。可以将多个小文件按照指定的顺序读取并写入到一个大文件中。需要注意的是,在写入时需要使用追加模式,避免覆盖已有的文件内容。 下面是一个简单的例子,实现了将大文件拆分成多个小文件和将多个小文件合并成一个大文件的功能: ```java import java.io.*; public class FileUtil { //拆分文件 public static void splitFile(String filePath, int splitSize) throws IOException { File file = new File(filePath); if (!file.exists() || file.isDirectory()) { throw new IOException("文件不存在或者是一个目录"); } long fileSize = file.length(); int fileNum = (int) Math.ceil((double) fileSize / splitSize); byte[] buffer = new byte[1024]; int len; try (RandomAccessFile raf = new RandomAccessFile(file, "r")) { for (int i = 0; i < fileNum; i++) { String fileName = file.getName() + "." + (i + 1); try (FileOutputStream fos = new FileOutputStream(fileName)) { int count = 0; while ((len = raf.read(buffer)) != -1) { fos.write(buffer, 0, len); count += len; if (count >= splitSize) { break; } } } } } } //合并文件 public static void mergeFile(String destPath, String... sourcePaths) throws IOException { File destFile = new File(destPath); if (destFile.exists()) { destFile.delete(); } try (FileOutputStream fos = new FileOutputStream(destFile, true)) { for (String sourcePath : sourcePaths) { File sourceFile = new File(sourcePath); if (!sourceFile.exists() || sourceFile.isDirectory()) { throw new IOException("文件不存在或者是一个目录"); } try (FileInputStream fis = new FileInputStream(sourceFile)) { byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } } } } } } ``` 使用方法: ```java //拆分文件 FileUtil.splitFile("test.txt", 1024 * 1024); //合并文件 FileUtil.mergeFile("test_merge.txt", "test.txt.1", "test.txt.2", "test.txt.3"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值