Java uses IO streams to split and merge large files.

Source: Internet
Author: User

Java uses IO streams to split and merge large files.

File splitting is a more practical function. For example, if you have a 3G file that needs to be copied from one computer to another, however, your storage device (such as the SD card) only has 1 GB. In this case, you can cut the file into three 1 GB files, copy them separately, and merge the three files, in this way, the problem is solved. For example, you have a file of hundreds of MB to be uploaded to FTP, however, this FTP limit cannot exceed 10 MB for a single file. At this time, you can use the File splitting method to solve the problem. Since it is split, we need to merge it when we use it again. Today we can use Java code to split and merge files. Now we can demonstrate a local file. The file directory is:Eclipse-jee-juno-win32.zip E: \(Let's take a good look at eclipse, which everyone hates today ):

Before split:

The split result is:

Java file splitting method:
1 // File segmentation method (the path of the File to be split and the number of parts to be split are passed in the method) 2 private static void splitFileDemo (File src, int m) {3 if (src. isFile () {4 // get the total length of the file 5 long l = src. length (); 6 // get the file name 7 String fileName = src. getName (). substring (0, src. getName (). indexOf (". "); 8 // get the file suffix 9 String endName = src. getName (). substring (src. getName (). lastIndexOf (". "); 10 System. out. println (endName); 11 InputStream in = null; 12 try {13 in = new File InputStream (src); 14 for (int I = 1; I <= m; I ++) {15 StringBuffer sb = new StringBuffer (); 16 sb. append (src. getParent ()). append ("\\"). append (fileName) 17. append ("_ data "). append (I ). append (endName); 18 System. out. println (sb. toString (); 19 File file2 = new File (sb. toString (); 20 // create the output stream of the Write File 21 OutputStream out = new FileOutputStream (file2); 22 int len =-1; 23 byte [] bytes = new byte [10*1024*1024]; 24 while (Len = in. read (bytes ))! =-1) {25 out. write (bytes, 0, len); 26 if (file2.length ()> (l/m) {27 break; 28} 29} 30 out. close (); 31} 32} catch (Exception e) {33 e. printStackTrace (); 34} 35 finally {36 try {37 in. close (); 38} catch (IOException e) {39 e. printStackTrace (); 40} 41} 42} 43}
Java file merging method:
1 // file merging method (transfer the file path to be merged) 2 private static void joinFileDemo (String... src) {3 for (int I = 0; I <src. length; I ++) {4 File file = new File (src [I]); 5 String fileName = file. getName (). substring (0, file. getName (). indexOf ("_"); 6 String endName = file. getName (). substring (file. getName (). lastIndexOf (". "); 7 8 StringBuffer sb = new StringBuffer (); 9 sb. append (file. getParent ()). append ("\\"). append (fileN Ame) 10. append (endName); 11 System. out. println (sb. toString (); 12 try {13 // read the input stream of a small file 14 InputStream in = new FileInputStream (file ); 15 // output stream 16 File file2 = new File (sb. toString (); 17 OutputStream out = new FileOutputStream (file2, true); 18 int len =-1; 19 bytes [] bytes = new byte [10*1024*1024]; 20 while (len = in. read (bytes ))! =-1) {21 out. write (bytes, 0, len); 22} 23 out. close (); 24 in. close (); 25} catch (Exception e) {26 e. printStackTrace (); 27} 28} 29 System. out. println ("file merging completed! "); 30}

I thought it was complicated before writing. After writing it, I thought that was the case. You can also practice it.

 

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.