Black Horse Programmer -Foundation framework NSFileManager introduction and usage

2023-03-16  

Use Java to write code to cut a large file into small files with specified size

Thinking:
Cutting operation of known files-> Get multiple fragmented files

Use:
1, 1 byte input stream -> Read the data in the known file
2, multiple byte output streams -> generate multiple fragments files

Thinking supplement:
Create a Byte array specified, read the large file into the Byte array, and write the Byte array into a new small file once, so that the large file is read until the large file reads the completion
Note: At this time, the last small file may be less than the specified memory size. When reading the last Byte array from a large file, you may not read the BYTE array. The unsatisfactory memory size byte is written into the last file;

specific code is as follows:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class CutDemo {
    
    public static void main(String[] args) {
        // Call the CUTFILE () function parameter (the original large file, the path of the small file stored after cutting, and the memory size specified by cutting)
        cutFile("D:\\java\\cut\\ForrestGump.avi", "D:\\java\\cuts",1024 * 1024 * 20);
    }

    private static void cutFile(String src, String endsrc, int num) {
        FileInputStream fis = null;
        File file = null;
        try {
            fis = new FileInputStream(src);
            file = new File(src);
            // Create a BYTE array specified size
            byte[] b = new byte[num];
            int len = 0;
            // name Prepare for future small file names
            int name = 1;
            // Traversing the large files into the BYTE array, when the Byte array is read, it is written into the corresponding small file
            while ((len = fis.read(b)) != -1) {
                // Find the file name and file type of the original large files, and prepare for the following small files
                String name2 = file.getName();
                int lastIndexOf = name2.lastIndexOf(".");
                String substring = name2.substring(0, lastIndexOf);
                String substring2 = name2.substring(lastIndexOf, name2.length());
                FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
                // Write the BYTE array into the corresponding small file
                fos.write(b, 0, len);
                // End resource
                fos.close();
                name++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    // End resource
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Screenshot of the corresponding path

Original large file path Screenshot:
这里写图片描述

Small files after cutting the code before the code of the code:

Screenshot of the path after the small file executes the small file after cutting:
这里写图片描述

这里写图片描述

source

Related Posts

EasyStorr, more elegant and convenient access to data objects, accessing the local area like MAP.

[python] [Original] OpenCV reads the camera or video template code

TORINGBOOT project is made into jar package and deployed on the Linux server

Big Data CDH Cluster Construction

Black Horse Programmer -Foundation framework NSFileManager introduction and usage

Random Posts

Detailed explanation SpringBoot Schedule configuration Amelia

Protective measures for vulnerabilities of Android application

PTA 11-Application and login of Shalement 3 QQ account (detailed explanation)

Springer LNCS Latex Submission Template How to use TO USE The Springer LNCS Latex Template

Oracle Clob field Insertation The character is too long