Java Learning Lesson 55th-io Stream (ix) file cutting synthesizer

Source: Internet
Author: User
Tags save file

File Cutters

private static final int SIZE = 1024x768 *1024;public static void Splitfile (file file) throws ioexception{//associated file with read stream (indeterminate file format) F Ileinputstream fis = new FileInputStream (file);//source is a byte[] by = new byte[size];//defines 1M buffers fileoutputstream fos = null;//Sinks Don't know how many int len = 0;int count = 1;//Records the number of sub-files file dir = new file ("D:\\patfiles"); if (!dir.isfile ()) {dir.mkdirs ();} while (len = Fis.read (by)!=-1) {fos = new FileOutputStream (The new File (dir, (count++) + ". Part"));//Custom file format fos.write (by,0, Len);} Fos.close (); Fis.close ();}


File merge

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<fileinputstream> AL = new arraylist< Fileinputstream> (); for (int i = 1;i<=7;i++) {al.add (new FileInputStream (New File (dir,i+ ")));} Enumeration<fileinputstream> en = collections.enumeration (AL); Sequenceinputstream sis = new Sequenceinputstream (en); FileOutputStream fos = new FileOutputStream (dir, "Midsummer Lightyear. mp3")); byte[] by = new Byte[1024];int len = 0;while (len = Sis.read (by))!=-1 {fos.write (by, 0, Len);} Sis.close (); Fos.close ();}

file cut merge + config 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;public static void Main (string[] args) Throws IOException {file File1 = new File ("D:\\needsplit\\ Midsummer Lightyear. mp3"); File File2 = new file ("D:\\partfiles"); Splitfile (File1); Merge_1 (file2);} public static void Splitfile (file file) throws ioexception{//is associated with the read stream file (not deterministic) fileinputstream FIS = new FileInputStream ( file);//source is a byte[] by = new byte[size];//defines a buffer of 1M fileoutputstream fos = null;//sinks do not know how many int len = 0;int count = 1;//Record the number of sub-files /* Cut files must record the name of the cut file and the number of fragmented files processed by the cut to facilitate merging * This information is used in order to describe the method of using key-value pairs, so use the Properties object */properties Pro = new properties (); File dir = new file ("D:\\partfiles"), if (!dir.isfile ()) {dir.mkdirs ();} while (len = Fis.read (by)!=-1) {fos = new FileOutputStream (The new File (dir, (count++) + ". Part"));//Custom file format fos.write (by,0, Len); Fos.close ();} Save the post-cut file information in the Pro collection Pro.setproperty ("Partcount", count+ "");p Ro.setproperTy ("FileName", File.getname ()); fos = new FileOutputStream (new file (dir,count+ ". Properties"));// Stores the information for the Pro collection in the collection Pro.store (FOS, "Save file Infmation"); Fis.close ();} public static void Merge_1 (file dir) throws ioexception{//gets the configuration file object under the specified directory file[] files = dir.listfiles (new Suffixfilter (". Properties ")//new a filter if (files.length!=1) {throw new RuntimeException (dir+" file with no properties extension in this directory or not unique ");} Record Profile object File Confile = files[0];//Get profile information Properties Pro = new properties (); FileInputStream fis = new FileInputStream (confile);//Correlation Stream Object Pro.load (FIS);//load information string filename = Pro.getproperty (" FileName ");//get the file name int count = Integer.parseint (Pro.getproperty (" Partcount "));//Get the number of fragments//Get all fragmented files//define filters in this directory Determine whether the number of fragmented files is consistent with fragmentation information in configuration information file[] partfiles = dir.listfiles (New Suffixfilter (". part")); if (partfiles.length!= (count-1) {throw new RuntimeException ("The number of fragmented files is incorrect, should be" +count+ "!) ");} Associates a fragmented file with a stream object and stores the collection in arraylist<fileinputstream> AL = new arraylist<fileinputstream> (); for (int i = 0;i< partfiles.length;i++) {Al.adD (New FileInputStream (Partfiles[i));} Combine multiple streams into a sequence flow enumeration<fileinputstream> en = collections.enumeration (AL); Sequenceinputstream sis = new Sequenceinputstream (en);//Read and write process FileOutputStream fos = new FileOutputStream (New File (dir, filename); byte[] by = new Byte[1024];int len = 0;while ((len = Sis.read (by))!=-1) {fos.write (by, 0, Len);} Sis.close (); Fos.close ();}}




Java Learning Lesson 55th-io Stream (ix) file cutting synthesizer

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.