Split and merge large files to achieve split and merge

Source: Internet
Author: User

Split and merge large files to achieve split and merge

1 public void cutFile (File bigFile, File destFile, int cutSize) {2 3 FileInputStream inputStream = null; 4 int size = 1024*1024; // 1 M 5 try {6 if (! DestFile. isDirectory () {// if the address for saving the split file is not the path 7 destFile. mkdir (); // create path 8} 9 size = size * cutSize; // split the file size in the unit of M, 10 int length = (int) bigFile. length (); // obtain the size of a large file (in Unit B) 11 int num = length/size; // calculate the number of small files to be split (the size of each small file is in MB) 12 int yu = length % size; // the size of the file to be divided (M) 13 14 String bigFilePath = bigFile. getAbsolutePath (); // obtain the complete path information of a large file (including the file name) 15 int fileNew = bigFilePath. lastIndexOf (". "); // get the". "Index 16 String suffix = bigFilePath. substring (fileNew, bigFilePath. length (); // get the suffix, that is, the file type 17 18 inputStream = new FileInputStream (bigFile ); // get large File input stream 19 File [] smallFile = new File [num + 1]; // create an array of files for saving small files 20 int begin = 0; 21 for (int I = 0; I <num; I ++) {22 smallFile [I] = new File (bigFile. getAbsolutePath () + "\" + (I + 1) + suffix + ". tem "); // specify the name of the small file 23 if (! SmallFile [I]. isFile () {24 smallFile [I]. createNewFile (); // create this file 25} 26 FileOutputStream outputStream = new FileOutputStream (smallFile [I]); // create a small file. The output stream is 27 byte [] small = new byte [size]; 28 inputStream. read (small); // read the small file byte 29 outputStream. write (small); // write byte data to small files 30 begin = begin + size; 31 outputStream. close (); 32} 33 if (yu! = 0) {// The remainder object size (M) is not empty. 34. smallFile [num] = new File (bigFile. getAbsolutePath () + "\" + (num + 1) + suffix + ". tem "); 35 if (! SmallFile [num]. isFile () {36 smallFile [num]. createNewFile (); // Create File 37} 38 FileOutputStream outputStream = new FileOutputStream (smallFile [num]); 39 byte [] bytes = new byte [yu]; 40 inputStream. read (bytes); // read the byte 41 outputStream. write (bytes); // write data to the file 42 outputStream. close (); 43} 44} catch (Exception e) {45 e. printStackTrace (); 46} 47}

2. merge processing

1 public void closeFile (File [] files, File closeDir, String hz) {2 try {3 File closeFile = new File (closeDir. getAbsoluteFile () + "\ close" + hz); // specify the merged file name (including path) 4 if (! CloseFile. isFile () {5 closeFile. createNewFile (); // create file 6} 7 8 FileOutputStream outputStream = new FileOutputStream (closeFile); // create file output stream 9 for (int I = 0; I <files. length; I ++) {10 FileInputStream inputStream = new FileInputStream (closeFile); // create a file input stream 11 int len = (int) files [I]. length (); // obtain the size of a single sub-file 12 byte [] bytes = new byte [len]; 13 inputStream. read (bytes); 14 outputStream. write (bytes); 15 inputStream. close (); 16} 17 outputStream. close (); 18} catch (Exception e) {19 e. printStackTrace (); 20} 21}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.