首页 > 其他分享 >RandomAccessFile 文件切片和合并

RandomAccessFile 文件切片和合并

时间:2023-05-04 10:56:15浏览次数:41  
标签:java String 合并 切片 RandomAccessFile file import size

package com.lzw.flieslice;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *  文件切片和文件合并
 */
public class FileSlice {

    // 文件合并
    private void fileComposite(String destDir) throws Exception {
        // 找到所有的文件切片
        Path path = Paths.get(destDir);
        List<Path> pathList = Files.list(path).sorted((o1, o2) -> {
            String f1 = o1.toString();
            String f2 = o2.toString();
            String f1Name = f1.substring(f1.lastIndexOf("\\") + 1, f1.lastIndexOf("."));
            String f2Name = f2.substring(f2.lastIndexOf("\\") + 1, f2.lastIndexOf("."));
            return Integer.parseInt(f1Name) - Integer.parseInt(f2Name);
        }).collect(Collectors.toList()); // 其实这个地方要做排序操作
        RandomAccessFile newFile = new RandomAccessFile(destDir + "test.rar", "rw");
        // 遍历所有的文件
        long position = 0;
        for (Path fp : pathList) {
            // 读取文件中的内容
            File file = fp.toFile();
            System.out.println(file.getName());
            RandomAccessFile accessFile = new RandomAccessFile(file, "r");
            FileChannel fileChannel = accessFile.getChannel();
            long fileSize = fileChannel.size();
            byte[] bs = new byte[(int) fileSize];
            accessFile.read(bs);
            newFile.seek(position);
            newFile.write(bs);
            position += fileSize;
            fileChannel.close();
        }
        newFile.close();
    }

    // 文件切割,每个文件分片大小为 1mb
    private void fileSlice(String srcPath, String destDir, long sliceSize) throws Exception {
        RandomAccessFile file = new RandomAccessFile(srcPath, "rw");
        // 获取文件大小
        long size = file.getChannel().size();

        long indexSeek = 0;

        int fileIndex = 1;

        while (size > 0) {
            // 文件slice size
            if (size < sliceSize) {
                sliceSize = size;
            }
            byte[] sliceByte = new byte[(int) sliceSize];
            file.seek(indexSeek);
            file.read(sliceByte);
            indexSeek = file.getChannel().position();
            size = size - sliceSize;
            // 保存切片文件
            RandomAccessFile newFile = new RandomAccessFile(destDir + fileIndex + ".rar", "rw");
            newFile.write(sliceByte);
            newFile.close();
            fileIndex ++;
        }

        file.close();
    }

    public static void main(String[] args) throws Exception {
        String srcPath = "D:/test/test.rar";
        String destDir = "D:/test/slice/";
        long sliceSize = 1024 * 1024 * 10; // 每个分片大小

        FileSlice fileSlice = new FileSlice();
        fileSlice.fileSlice(srcPath, destDir, sliceSize);
        fileSlice.fileComposite(destDir);
    }
}

 

标签:java,String,合并,切片,RandomAccessFile,file,import,size
From: https://www.cnblogs.com/xiaowenwen/p/17370410.html

相关文章

  • 有序序列合并
    题目链接有序序列合并学归并排序之前练习题解#include<bits/stdc++.h>usingnamespacestd;inta1[1010],a2[1010],a[2020];intmain(){intn,m;cin>>n>>m;for(inti=1;i<=n;i++)cin>>a1[i];for(inti=1;i<=m;i++)cin>>a......
  • python 合并json
    importjson#foriinrange(800):f1=open('.\\'+str(0)+'.txt',"r")data1=f1.read()dic_str2=json.loads(str(data1).replace("'","\""))foriinrange(1,50):f=open('.\\'+......
  • 线段树合并/分裂
    你说的对,但是你理应会动态开点线段树是什么东西。合并很简单,两棵线段树一块搜,然后逐个节点合并。分裂的话可以按照FHQTreap的方法。假如我们将前\(k\)小和后边分开成\(x,y\),首先看左子树,如果比\(k\)大那右子树给\(y\),递归左子树,反之左子树给\(x\),递归右子树。真没啥......
  • 字典合并;函数返回值同时用于判断与输出
    1#普通字典update,与Counterupdate不同2d1={"1":2,"2":2}3d2={"1":1,"2":2}4print(d2)5#{'1':1,'2':2}6d2.update(d1)7print(d2)8#{'1':2,'2':2}9fromcollectio......
  • 代码笔记27 numpy和pytorch中的多维数组切片
    原来还可以用数组切数组,我算是长见识了。不多说了,直接上代码应该可以明白importnumpyasnpxyz=np.arange(36).reshape(3,4,3)B,N,C=xyz.shapefarthest=np.random.randint(0,N,size=B)#torch.randint(0,N,(B,),dtype=torch.long)#初始时随机选择一点(B......
  • rust中动态数组的引用和切片
    真逆天这个b语法1切片与String切片类似,动态数组Vec也能切片,通过&取切片般如果Vec是可变的话,那么他的切片就是不可变的/只读的注意:切片和&Vec是不同的类型,后者仅仅是Vec的引用,并可以通过解引用直接获取Vecfnmain(){letmutv=vec![1,2,3];letslice=&......
  • Halcon轮廓的分割,合并及圆&矩形&线拟合
    变换平滑轮廓:smooth_contours算子:smooth_contours_xld(Contours : SmoothedContours : NumRegrPoints :)示例:smooth_contours_xld(Border,SmoothedContours,11)Border(输入对象):输入轮廓对象SmoothedContours(输出对象):输出平滑后的轮廓11(输入控制参数):数值越大越平滑......
  • Halcon轮廓的分割,合并及圆&矩形&线拟合
    变换 平滑轮廓:smooth_contours算子:smooth_contours_xld(Contours : SmoothedContours : NumRegrPoints :)示例:smooth_contours_xld(Border,SmoothedContours,11)Border(输入对象):输入轮廓对象SmoothedContours(输出对象):输出平滑后的轮廓11(输入控制参数):数值越大越平滑形状变换......
  • CF 1709E XOR Tree(树上启发式合并)
    题目链接:https://codeforces.com/contest/1709/problem/E解题思路:定义sum(x,y)为x→y路径上的点的异或和,dx 为x→root路径上的点的异或和。对于一个点权树,sum(x,y)=dx ^dy ^vallca(x,y)。考虑修改一个点,可以将它改为一个很大的2为底数的幂,则经过此点的所有的不合......
  • 树上启发式合并学习笔记
    最近几天了解到一个很神奇的算法——dsuontree,看上去没多快实际上很快,这叫低调。好久不更了,至于反演,5月再更吧,4月的最后一天分享一下dsuontree。顺便闲话一句,4/26是我生日,也是历史二模。重链剖分dsuontree这类dsuontree适用于多次询问,每次询问需要$O(子树大小)......