Sequenceinputstream can be seen as an ordered set of multiple inputstream objects. When the data of an inputstream object is read, it automatically retrieves the next inputstream object for reading until all the inputstream objects are read.
With this feature, we will compile a file splitting and merging tool.
Instructions for use:
Sequencedemo [Option] [filepath] [number]
Option:
-C: indicates the file to be merged.
-S: indicates the file to be split.
Filepath:
Specify a file path
Number:
Specifies the size or number of files.
For example:
java cls.SequeceDemo -s F:\QQ.exe 102400
Segment qq.exe into small files with a size of 102400 bytes.
java cls.SequeceDemo -m F:\QQ.exe 13
Combine the split 13 qq.exe files into an entire file.
Note: When constructing a sequenceoutputstream object, an enumeration object must be referenced as a parameter. Enumeration is an interface. We must implement the hasmoreelements () and nextelement () methods in this interface.
Complete code:
Package CLS; import Java. io. *; import Java. util. *; public class sequencestreamdemo {public static void main (string [] ARGs) throws exception {Switch (ARGs [0]. charat (1) {Case's ': // split the file {sequencestreamdemo. seperate (ARGs [1], long. parselong (ARGs [2]); break;} case 'M': // merge the file {sequencestreamdemo. merge (ARGs [1], integer. parseint (ARGs [2]); break;} default: // parameter input error {system. out. println ("invalid parameter"); brea K ;}}/ * Merged file * filename: file name * Amount: Number of small files to be merged **/public static void Merge (string filename, int amount) throws exception {// create the merged file = new file (filename); fileoutputstream FD = new fileoutputstream (File); bufferedoutputstream Bos = new bufferedoutputstream (FS ); // save multiple small file objects in the list. List <inputstream> List = new arraylist <inputstream> (); For (INT I = 0; I <amount; ++ I) {File F = new file (filename + "_" + I); list. add (New fileinputstream (f);} final iterator <inputstream> it = List. iterator (); // construct enumeration object enumeration <inputstream> em = new enumeration <inputstream> () {public Boolean hasmoreelements () {return it. hasnext ();} public inputstream nextelement () {return it. next () ;}}; // use sequenceinputstream to merge the file bufferedinputstream Bis = new bufferedinputstream (ne W sequenceinputstream (EM), 8192); int data; while (Data = bis. Read ())! =-1) {Bos. write (data);} bis. close (); Bos. flush (); Bos. close (); system. out. println ("merged");}/* Split file * filename: file name * size: size of a single file */public static void seperate (string filename, Long SIZE) throws exception {file = new file (filename); long number; // calculate the number of split files if (0 = file. length () % size) {number = file. length ()/size;} else {number = file. length ()/size + 1;} fileinputstream F Is = new fileinputstream (File); bufferedinputstream Bis = new bufferedinputstream (FCM); // start to split byte [] Buf = new byte [1]; for (INT I = 0; I <number; ++ I) {fileoutputstream Fos = new fileoutputstream (new file (filename + "_" + I); bufferedoutputstream Bos = new bufferedoutputstream (FOS ); long cursize = 0l; while (BIS. read (BUF )! =-1) {Bos. write (BUF); cursize ++; If (cursize = size) // It has been split into a small file {Bos. flush (); Bos. close (); break ;}}if (cursize <size) // package the remaining data {Bos. flush (); Bos. close (); break;} system. out. println ("segmentation completed ");}}