如何使用Java将文件分解成碎片? [英] How to break a file into pieces using Java?

查看:15
本文介绍了如何使用Java将文件分解成碎片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题要问 - 如何使用 Java(任何文件类型)将给定的文件分成小块?然后我可以将这些作品放在许多 CD 和/或随身碟上,然后将它们拿走.我尝试以这种方式>

但正如大多数用户评论的那样,我尝试的方法无法通过这种方式实现.所以,我决定提出一个新问题来获得正确的文件破解方法.

当我将一个文件分成几块(比如 30 块)时,必须有一种方法可以重新组合它们并重新创建原始文件.请帮忙.

解决方案

当缓冲区已满或文件末尾时,读取部分字节到字节数组并将它们存储在新文件中.

例如(代码并不完美,但应该有助于理解过程)

class FileSplit {public static void splitFile(File f) 抛出 IOException {int partCounter = 1;//我喜欢从001, 002, 003, ...//如果你想要 000, 001, ... 你可以把它改成 0int sizeOfFiles = 1024 * 1024;//1MB字节 [] 缓冲区 = 新字节 [sizeOfFiles];字符串文件名 = f.getName();//try-with-resources 以确保关闭流试试 (FileInputStream fis = new FileInputStream(f);BufferedInputStream bis = 新的 BufferedInputStream(fis)) {int bytesAmount = 0;while ((bytesAmount = bis.read(buffer)) > 0) {//将每个数据块写入不同名称的不同文件中String filePartName = String.format("%s.%03d", fileName, partCounter++);File newFile = new File(f.getParent(), filePartName);试试 (FileOutputStream out = new FileOutputStream(newFile)) {out.write(buffer, 0, bytesAmount);}}}}public static void main(String[] args) 抛出 IOException {splitFile(new File("D:\destination\myFile.mp4"));}}

myFile.mp4 大小=12,7 MB

拆分后我有 13 个文件

  • myFile.mp4.001 - myFile.mp4.012 大小为 1 MB
  • myFile.mp4.013 大小为 806 KB
<小时>

如果你想合并这些文件,你可以使用

public static void mergeFiles(List files, File into)抛出 IOException {试试 (FileOutputStream fos = new FileOutputStream(into);BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) {for (文件 f : 文件) {Files.copy(f.toPath(), mergingStream);}}}

您还可以创建一些额外的方法来让您的生活更轻松.例如,将根据这些文件之一的名称(和位置)创建包含分离部分的文件列表的方法.

公共静态列表<文件>listOfFilesToMerge(文件 oneOfFiles){String tmpName = oneOfFiles.getName();//{name}.{number}String destFileName = tmpName.substring(0, tmpName.lastIndexOf('.'));//删除.{number}File[] 文件 = oneOfFiles.getParentFile().listFiles((文件目录,字符串名称)->name.matches(destFileName + "[.]\d+"));Arrays.sort(files);//确保顺序001, 002, ..., 010, ...返回 Arrays.asList(files);}

使用该方法,我们可以重载 mergeFiles 方法以仅使用其中一个文件 File oneOfFiles 而不是整个列表 List (我们将根据其中一个文件生成该列表)

public static void mergeFiles(File oneOfFiles, File into)抛出 IOException {合并文件(listOfFilesToMerge(oneOfFiles),进入);}

您也可以重载这些方法以使用 String 而不是 File(我们会在需要时将每个 String 包装在 File 中)

公共静态列表<文件>listOfFilesToMerge(字符串 oneOfFiles){返回 listOfFilesToMerge(new File(oneOfFiles));}public static void mergeFiles(String oneOfFiles, String into) 抛出 IOException{合并文件(新文件(oneOfFiles),新文件(进入));}

I got a kind of weird question to ask - how can I break a given file into small pieces using Java (any file type)? Then I could put those pieces onto a number of CD's and/or pendrives and take them away. I tried doing it this way

But as most users commented, what I am trying is not achievable in that way. So, I decided to ask a new question for getting a correct method for breaking files.

When I break a file into pieces (say 30 pieces), there must be a way to reassemble them and recreate the original file. Please help.

解决方案

Read portions of bytes to byte array and store them in new files when buffer is full or it is end of file.

For example (code is not perfect, but it should help understanding the process)

class FileSplit {
    public static void splitFile(File f) throws IOException {
        int partCounter = 1;//I like to name parts from 001, 002, 003, ...
                            //you can change it to 0 if you want 000, 001, ...

        int sizeOfFiles = 1024 * 1024;// 1MB
        byte[] buffer = new byte[sizeOfFiles];

        String fileName = f.getName();

        //try-with-resources to ensure closing stream
        try (FileInputStream fis = new FileInputStream(f);
             BufferedInputStream bis = new BufferedInputStream(fis)) {

            int bytesAmount = 0;
            while ((bytesAmount = bis.read(buffer)) > 0) {
                //write each chunk of data into separate file with different number in name
                String filePartName = String.format("%s.%03d", fileName, partCounter++);
                File newFile = new File(f.getParent(), filePartName);
                try (FileOutputStream out = new FileOutputStream(newFile)) {
                    out.write(buffer, 0, bytesAmount);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        splitFile(new File("D:\destination\myFile.mp4"));
    }
}

myFile.mp4 size=12,7 MB

After split I had 13 files

  • myFile.mp4.001 - myFile.mp4.012 with size 1 MB
  • myFile.mp4.013 with size 806 KB

If you want to merge these files you can use

public static void mergeFiles(List<File> files, File into)
        throws IOException {
    try (FileOutputStream fos = new FileOutputStream(into);
         BufferedOutputStream mergingStream = new BufferedOutputStream(fos)) {
        for (File f : files) {
            Files.copy(f.toPath(), mergingStream);
        }
    }
}

You can also create some additional methods to make your life easier. For instance method which will create list of files containing separated parts based on name (and location) of one of these files.

public static List<File> listOfFilesToMerge(File oneOfFiles) {
    String tmpName = oneOfFiles.getName();//{name}.{number}
    String destFileName = tmpName.substring(0, tmpName.lastIndexOf('.'));//remove .{number}
    File[] files = oneOfFiles.getParentFile().listFiles(
            (File dir, String name) -> name.matches(destFileName + "[.]\d+"));
    Arrays.sort(files);//ensuring order 001, 002, ..., 010, ...
    return Arrays.asList(files);
}

With that method we can overload mergeFiles method to use only one of the files File oneOfFiles instead of whole list List<File> (we will generate that list based on one of the files)

public static void mergeFiles(File oneOfFiles, File into)
        throws IOException {
    mergeFiles(listOfFilesToMerge(oneOfFiles), into);
}

You can also overload these methods to use String instead of File (we will wrap each String in File when needed)

public static List<File> listOfFilesToMerge(String oneOfFiles) {
    return listOfFilesToMerge(new File(oneOfFiles));
}

public static void mergeFiles(String oneOfFiles, String into) throws IOException{
    mergeFiles(new File(oneOfFiles), new File(into));
}

这篇关于如何使用Java将文件分解成碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆