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

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

问题描述

我有一个奇怪的问题要问。我需要知道如何使用Java(任何文件类型)将给定文件分成小块。然后,我可以将这些和平放入CD,pendrives并带走。我尝试使用这种方式

I got a kind of weird question to ask. I need to know how can I break a given file into small small pieces using Java (any file type). Then I can put those peaces into number of CD's, pendrives and take away. I tried it using 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.

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

When I break a file into pieces (guess 30 pieces), there must be a way to re assemble them back and create the original file back. 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

myFile.mp4 size=12,7 MB

分割后我有13个文件


  • myFile.mp4 .001 - myFile.mp4.012 ,大小为1 MB

  • myFile .mp4.013 ,大小为806 KB

  • 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);
}

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

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);
}

您还可以重载这些方法以使用 String 而不是文件(我们将在需要时将每个字符串包装在文件中)

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天全站免登陆