JAVA learning lesson 55th-IO stream (9) file cutting Synthesizer

Source: Internet
Author: User

JAVA learning lesson 55th-IO stream (9) file cutting Synthesizer

File Cutter

Private static final int SIZE = 1024*1024; public static void splitFile (File file) throws IOException {// read the stream associated File (the file format is not determined) fileInputStream FCM = new FileInputStream (file); // the source is a byte [] by = new byte [SIZE]; // defines the 1 m buffer FileOutputStream fos = null; // do not know how many int len = 0; int count = 1; // record the number of subfiles File dir = new File ("D: \ patFiles "); if (! Dir. isFile () {dir. mkdirs () ;}while (len = Fi. read ())! =-1) {fos = new FileOutputStream (new File (dir, (count ++) + ". part "); // custom file format fos. write (by, 0, len);} fos. close (); FCM. close ();}


File Merging

Public static void main (String [] args) throws IOException {File file = new File ("D: \ PartFile"); Merge (file );} public static void Merge (File dir) throws IOException {ArrayList
 
  
AL = new ArrayList
  
   
(); For (int I = 1; I <= 7; I ++) {AL. add (new FileInputStream (new File (dir, I + ". part ");} Enumeration
   
    
En = Collections. enumeration (AL); SequenceInputStream sis = new SequenceInputStream (en); FileOutputStream fos = new FileOutputStream (new File (dir, ".mp3 ")); byte [] by = new byte [1024]; int len = 0; while (len = sis. read ())! =-1) {fos. write (by, 0, len);} sis. close (); fos. close ();}
   
  
 

File cutting and merging + configuration file

Import java. io. *; import java. util. arrayList; import java. util. collections; import java. util. enumeration; import java. util. properties; public class Main {private static final int SIZE = 1024*1024; public static void main (String [] args) throws IOException {File file1 = new File ("d: \ NeedSplit \ .mp3 "); File file2 = new File (" D: \ PartFiles "); splitFile (file1); Merge_1 (file2);} public static void splitFile (File file) throws IOException {// read the stream associated file (uncertain File Format) FileInputStream FD = new FileInputStream (file ); // The Source is a byte [] by = new byte [SIZE]; // defines the buffer FileOutputStream fos = null for 1 MB; // The number of int len = 0 unknown; int count = 1; // record the number of sub-files/* The cut file must record the name of the cut file and the number of fragment files processed by the cut, to facilitate the merge * This information for description, use the key-Value Pair method, so use the Properties object */Properties pro = new Properties (); File dir = new File ("D: \ PartFiles"); if (! Dir. isFile () {dir. mkdirs () ;}while (len = Fi. read ())! =-1) {fos = new FileOutputStream (new File (dir, (count ++) + ". part "); // custom file format fos. write (by, 0, len); fos. close () ;}// Save the information of the cut file in the pro collection. setProperty ("partCount", count + ""); pro. setProperty ("fileName", file. getName (); fos = new FileOutputStream (new File (dir, count + ". properties "); // store the information of the pro set in the collection pro. store (fos, "save file infmation"); FCM. close ();} public static void Merge_1 (File dir) throws IOException {// get the specified File [] files = dir. listFiles (new SuffixFilter (". properties"); // new Filter if (files. length! = 1) {throw new RuntimeException (dir + "files without the properties extension in this directory or not unique");} // record the configuration File object File confile = files [0]; // obtain the configuration file information Properties pro = new Properties (); FileInputStream FD = new FileInputStream (confile); // associate the stream object pro. load (FCM); // load information String filename = pro. getProperty ("fileName"); // get the file name int count = Integer. parseInt (pro. getProperty ("partCount"); // get the number of shards // get all the fragment files in the directory // define the filter, determine whether the number of fragment files is consistent with the fragment information in the configuration information. File [] par TFiles = dir. listFiles (new SuffixFilter (". part"); if (partFiles. length! = (Count-1) {throw new RuntimeException ("the number of fragment files is incorrect. It should be" + count +! ");} // Associate the fragment file with the stream object and coexist in the ArrayList
 
  
AL = new ArrayList
  
   
(); For (int I = 0; I
   
    
En = Collections. enumeration (AL); SequenceInputStream sis = new SequenceInputStream (en); // FileOutputStream fos = new FileOutputStream (new File (dir, filename )); byte [] by = new byte [1024]; int len = 0; while (len = sis. read ())! =-1) {fos. write (by, 0, len) ;}sis. close (); fos. close ();}}
   
  
 




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.