Java basics-file Cutter

Source: Internet
Author: User

Well, since the advanced problem of algorithms cannot be easily solved, consolidate the foundation first!

I will review my previous knowledge based on my learning experience!

First of all, I declare that these concepts are not created by myself and are introduced in my learning process. Then, based on my understanding, the idle time is completed independently. All source code is original! This is the result of standing on the shoulders of giants.

Write a file cutter today:

The file cutter is used to cut some large files into subfiles of the same size (except the tail. Why? This is because in some forums and other websites, the size of uploaded files cannot exceed a certain size due to server performance and other reasons. In order to upload some large files, we have to cut these large files into a small file. After downloading these files, you can combine them to restore them to their original files.

The core of the file cutter is the Java Io stream. Because the file is stored on the hard disk, the file to be cut may be a video stream or audio stream file, therefore, byte streams are generally used. To read files from the hard disk to the memory, it is obvious that fileinputstream is used as the stream object. Because we need to cut the file according to the specified size, we should manually define the buffer, so we will not wrap this stream.

How can we exile a large file into a small file stream? Here, we need to use a loop. Each time the buffer data is promptly flushed to fileoutputstream and the file is named.

With regard to file naming, in order not to overwrite the previous data, we can use a unique data to name it. This reminds us of the timestamp. You can name the millisecond value of the current system time. If you think the data is too big, you can get the remainder. Another simple method is to create a counter and use the original file name + counting serial number as the file name of the fragment file. This method also works. It is more practical. So I use this master. The file Suffix in window is used to differentiate the file data of different applications. No other function. Therefore, you can customize a unique suffix here. However, we generally use. part as the suffix, which is easy to understand.

During the cutting process, you should pay attention to the merging problem after cutting. To merge a fragment file, you must know the file name, suffix, cut score, and so on. Therefore, the data should be stored in a file during the cutting process. Because the data is small and simple. Therefore, you can use a properties file for storage.

The general idea is like this. For the specific implementation process, we can directly look at the source code:

/*** Mar 28,201 3 * copyright (c) jackwang * all rights reserve * @ author <a href = "mailto: wangchengjack@163.com"> jackwang </a> */package COM. myjava. function; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. printstream; import Java. util. properties; import COM. myjava. myexception. dataerrorexception; Import COM. myjava. myexception. nosuchfileexception;/*** large file cutting ** train of thought: the file type is uncertain, so you should use Word throttling * to customize the buffer, the buffer size is the size of the cut part * when saving the cut stream to a file * there should be a file used to record the cut information, such as the original file name, number of cut parts, read the information when merging files to determine the file type. * The suffix of the cut file can be customized. PART * @ author Wangcheng **/public class splitfiles {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {file = new file ("I:" + file. separator + "Liang zhuyun" ); // Defines the file object boolean result = splitfiles (file, 1); system. out. println (result );} /***** @ Param file the object to be cut * @ Param I the size to be cut * @ return whether the cut is successful * @ throws ioexception */public static Boolean splitfiles (File, int I) throws ioexception {// check if (! File. exists () {Throw new nosuchfileexception ("the file does not exist. Please check it !! ");} If (I <0) {Throw new dataerrorexception (" the data is invalid! ");} // File dir = new file (" I: "+ file. Separator +" partfiles ") at the storage location of the cut file; // customize the storage path of the fragment file if (! Dir. exists () {dir. mkdirs () ;}/// create the original file stream fileinputstream FCM = new fileinputstream (File); // create a buffer byte [] buff = new byte [1024*1024 * I]; int Len = 0; int COUNT = 0; // records the number of cut parts string filename = getfilename (file. getname (); While (LEN = Fi. read (buff ))! =-1) {fileoutputstream Fos = new fileoutputstream (new file (Dir, filename + (++ count) + ". part "); FOS. write (buff, 0, Len); FOS. close () ;}// record file information recordfileinfo (file. getname (), count); FCM. close (); Return true;} public static void recordfileinfo (string filename, int count) throws filenotfoundexception, ioexception {properties prop = new properties (); file countfile = new file ("I:" + file. separator + "partfiles", getfilename (filename) + ". properties "); prop. setproperty ("FILENAME", filename); prop. setproperty ("count", Count + ""); printstream out = new printstream (countfile); prop. store (Out, "Sava file infomation"); // prop. list (system. out);} public static string getfilename (string filename) {return filename. substring (0, filename. indexof (". "));}}

For the custom exception thrown in the code, I defined an exception package that inherits the runtimeexception class. If there is no technical content, I will not post it!

Now, the core code of the file cutter has been completed. Thank you for your criticism and guidance!

File merge tomorrow!

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.