1 Packageom.gh.homework;2 ImportJava.io.*;3 /**4 * Implement the segmentation file;5 * @paramfile6 */7 Public classHomework {8 /**9 * @paramsrcTen * The file path to be split One * @paramN A * The size of each file in megabytes - * @paramdesc - * Split file storage path the * @throwsFileNotFoundException - */ - Public Static voidFilesplit (File src,intMB, File desc) - throwsFileNotFoundException { + //determine file path - if(Src.exists () && src.isfile () &&desc.isdirectory ()) { + intfileSize = MB * 1024 * 1024; A intN; at //determine the number of files to be divided by N; - if(Src.length ()% FileSize = = 0) -n = (int) (Src.length ()/fileSize); - Else -n = (int) (Src.length ()/fileSize) + 1; - Try { inInputStream is =NewFileInputStream (SRC); -Bufferedinputstream bis =NewBufferedinputstream (IS); to for(inti = 0; I < n; i++) {//Loop writes each file; + byte[] B =New byte[fileSize]; -String newfile = Desc.getpath () +File.separator the+ src.getname () + "_" + i + ". Dat"; *Bufferedoutputstream BOS =NewBufferedoutputstream (//cache stream $ NewFileOutputStream (NewFile));Panax Notoginseng intLen =-1; - intCount=0; the while(len = Bis.read (b))! =-1) { +Bos.write (b,0, Len); Acount+=Len; the Bos.flush (); + if(count>=filesize) Break; - } $ bos.close (); $ } - bis.close (); - is.close (); the}Catch(FileNotFoundException e) { - e.printstacktrace ();Wuyi}Catch(IOException e) { the e.printstacktrace (); - } Wu}Else { - Throw NewFileNotFoundException ("The file does not exist or the parameter is wrong!") "); About } $ } - - Public Static voidMain (string[] args) { -File src =NewFile ("f:\\ movie \ \ Never thought of. mp4");//the target file to split AFile desc =NewFile ("f:\\ video \ \");//split the stored path + intMB = 500;//The size of each file, in megabytes; theSystem.out.println ("Start splitting ..."); - Try { $ filesplit (src, MB, desc); the}Catch(FileNotFoundException e) { the e.printstacktrace (); the } theSystem.out.println ("Split complete! "); - } in}
1 Packageom.gh.homework;2 ImportJava.io.*;3 /**4 * Merge the split files5 * 6 * @authorGanhang7 * 8 */9 Public classHOMEWORK2 {Ten /** One * A * @paramdesc - * The file path after synthesis - * @paramsrc the * List of files to be synthesized - */ - Public Static voidmerge (file desc, file ... src) { - Try { +String filename = src[0].getname (). substring (0, -Src[0].getname (). LastIndexOf ("_")); +File NewFile =NewFile (Desc.getpath () + File.separator +filename); AOutputStream OS =NewFileOutputStream (newfile); atBufferedoutputstream BOS =Newbufferedoutputstream (OS); - for(inti = 0; i < src.length; i++) { -Bufferedinputstream bis =NewBufferedinputstream ( - NewFileInputStream (Src[i])); - byte[] B =New byte[1024 * 1024]; - intLen =-1; in while(len = Bis.read (b))! =-1) { -Bos.write (b, 0, Len); to Bos.flush (); + } - bis.close (); the } * bos.close (); $ os.close ();Panax NotoginsengSYSTEM.OUT.PRINTLN ("Synthetic success! "); -}Catch(FileNotFoundException e) { the e.printstacktrace (); +}Catch(IOException e) { A e.printstacktrace (); the } + } - Public Static voidMain (string[] args) { $File desc=NewFile ("f:\\ video \ \"); $File [] src={ - NewFile ("f:\\ video \ \ \ I never thought of it. Mp4_0.dat"), - NewFile ("f:\\ video \ \ \ I never thought of it. Mp4_1.dat"), the NewFile ("f:\\ video \ \ \ I never thought of it. Mp4_2.dat"), - NewFile ("f:\\ video \ \ \ I never thought of it. Mp4_3.dat")Wuyi }; theSystem.out.println ("Start crafting ..."); - merge (DESC,SRC); Wu } -}
Java Learning IO File segmentation