Input/output stream in javaSE-one read stream corresponds to multiple output streams. The generated part files are numbered in sequence.

Source: Internet
Author: User

Input/output stream in javaSE-one read stream corresponds to multiple output streams. The generated part files are numbered in sequence.

Package com. io. other. split; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. util. properties; public class SplitFileTest {/*** file cutter. * One read stream corresponds to multiple output streams. The generated part files are numbered in an orderly manner * @ param args * @ throws IOException */private static final int DEFAULT_SIZE = 1024*2; public static void main (String [] args) throws IOException {File srcFile = new File (chen \ 11.avi); File destDir = new File (chen \ partfiles); SplitFile (srcFile, destDir );} /***** @ param srcFile * @ throws IOException */public static void SplitFile (File srcFile, File destDir) throws IOException {if (! SrcFile. exists () {throw new RuntimeException (srcFile. getAbsolutePath () +, the source file does not exist);} if (! DestDir. exists () {destDir. mkdirs ();} // 1. Define a source file, FileInputStream (FS) = new FileInputStream (srcFile); // 2. Create a target reference, FileOutputStream fos = null; // 3. Create a buffer to store data byte [] buf = new byte [DEFAULT_SIZE]; int len = 0; int count = 0; while (len = Fi. read (buf ))! =-1) {// create the output stream and specify the File to be written in File partFile = new File (destDir, (++ count) +. part); fos = new FileOutputStream (partFile); fos. write (buf, 0, len); fos. close () ;}// when a part file is generated, a configuration file must be generated to record the number of parts and the source file name // partfile = n; filename = 11.avi// key value information stored in the configuration file. Use the Properties set Properties prop = new Properties (); prop. setProperty (partcount, Integer. toString (count); prop. setProperty (filename, srcFile. getName (); File conFile = new File (destDir, (++ count) +. properties); fos = new FileOutputStream (conFile); // close the resource fos. close (); FCM. 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.