First, file segmentation
Ii. merger of documents
Way One : by appending the file
mode two : Through the SequenceInputStream
logical concatenation of the other input streams.
Testing RandomAccessFile
Random Access files
PackageFilesplitmerge;ImportJava.io.File;ImportJava.io.IOException;ImportJava.io.RandomAccessFile;ImportIoothers.fileutil;/** * Randomaccessfile * Instances of this class support read and write to random-access files. * * File Segmentation Ideas * 1, block number of blocks size n block * 2, size of each block blocksize * Last piece: Total File Size-(n-1) *blocksize */ Public class Demo01 { Public Static void Main(string[] args)throwsIOException {Randomaccessfile rnd =NewRandomaccessfile (NewFile ("G:/writer.txt"),"R");//utf-8--> English use 1 byte, Chinese use 3 bytes to encode //gbk--> takes 2 bytes per character //From the 12th reading this is the use of UTF-8Rnd.seek ( A);//define buffer size byte[] Flush =New byte[1024x768];//Receive length intlen=0; while(-1! = (Len=rnd.read (flush))) {if(len>= -) {System.out.println (NewString (Flush,0, -)); }Else{System.out.println (NewString (Flush,0, Len)); }} fileutil.close (RND); }}
Operation Result:
都有青春,每个青春都有一个故事?re ESX资源管理与性能优化S1.1.11VMware??FT-Practise实验演示7:vApp-Practise?
Splitting and merging of files
PackageFilesplitmerge;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.RandomAccessFile;ImportJava.io.SequenceInputStream;ImportJava.util.ArrayList;ImportJava.util.List;ImportJava.util.Vector;ImportIoothers.fileutil;@SuppressWarnings("All") Public class splitfile { //path to file PrivateString FilePath;//Block number Private intSize//size of each block Private LongBlockSize//File name PrivateString FileName;//File size Private LongLength//post-split storage directory PrivateString Destblockpath;//The name of each block PrivateList<string> Blockpath; Public Splitfile() {Blockpath =NewArraylist<string> (); } Public Splitfile(String filepath,string Destblockpath) { This(FilePath,1024x768, Destblockpath); } Public Splitfile(String FilePath,LongBlocksize,string Destblockpath) { This(); This. FilePath = FilePath; This. Destblockpath = Destblockpath; This. blocksize = blocksize; }/** * Initialize operation calculate block number, determine filename */ Public void Init() {File src =NULL;//Robustness creation will get the value initialized by the constructor method if(NULL==filepath | | ! (src=NewFile (FilePath)). Exists ()) {return; }if(Src.isdirectory ()) {return; }//filename g:/writer.txt writer.txt This. FileName = Src.getname ();//size of file This. length = Src.length ();//fix the size of each block if( This. blocksize>length)//If the size of each block is greater than the length of the text, the size of each block = length{ This. blocksize = length; }//Determines the number of blocks ceil the minimum (nearest negative infinity) floating-point value, which is greater than or equal to the parameter and equals an integer. Size = (int) Math.ceil (length*1.0/ This. blocksize);//Determine the path of the fileInitpathname (); }Private void Initpathname() { for(intI=0; i<size;i++) {the path of each block is added to the//list container. This. Blockpath.add (destblockpath+"/"+ This. filename+". Part"+i); } }/** * File Segmentation * Determine the number of blocks * 1, start position * 2, actual size * @param destpath partition file directory * @throws I Oexception * * Public void Split(String DestPath)throwsIOException {//Determine the path of the file //Name of each block //initpathname (destpath); LongBeginpos =0;//Starting point LongActualblocksize = blocksize;//Actual size //Calculate all the fast size, position, index for(intI=0;i<size; i++) {if(i = = size-1) {//last pieceActualblocksize = This. Length-beginpos; }///Specific segmentation method the first block of the initial partition address the actual block sizeSplitdetail (i,beginpos,actualblocksize); Beginpos + = Actualblocksize;//The end of this time, the beginning of the next} }/** * File split input/output * File copy * @param idx first few blocks * @param beginpos start point * @param actualblocksize actual Size * @throws IOException * / Public void Splitdetail(intIdxLongBeginpos,LongActualblocksize)throwsIOException {///1, create sourceFile src =NewFile ( This. FilePath);//source file //Get the path of the first block list container to take out the block pathFile dest =NewFile ( This. Blockpath.get (IDX));//destination file //2, select StreamRandomaccessfile RAF =NULL;//input streamBufferedoutputstream BOS =NULL;//output stream Try{RAF =NewRandomaccessfile (SRC,"R"); BOS =NewBufferedoutputstream (NewFileOutputStream (dest));///3, read fileRaf.seek (Beginpos);//4, Buffer byte[] Flush =New byte[1024x768];intLen =0; while(-1! = (Len=raf.read (flush))) {//write out if(actualblocksize-len>=0)//Determine if it is sufficient{Bos.write (flush,0, Len);//write outActualblocksize-= Len;//Amount remaining}Else{//Read the last part of the actual size of each piece last writtenBos.write (Flush,0,(int) actualblocksize); Break;////After the last part of each block is read, be sure to break or Continue reading} } }Catch(FileNotFoundException e) {E.printstacktrace (); }finally{fileutil.close (BOS,RAF); } }/** * File Merge (method one) */ Public void Merge1(String DestPath) {//Create sourceFile dest =NewFile (DestPath);//Select StreamBufferedoutputstream BOS =NULL;//output streamBufferedinputstream bis =NULL;//input stream Try{BOS =NewBufferedoutputstream (NewFileOutputStream (dest,true));//indicates append for(intI=0; i< This. Blockpath.size (); i++) {//Readbis =NewBufferedinputstream (NewFileInputStream (NewFile ( This. Blockpath.get (i)));//Buffer byte[] Flush =New byte[1024x768];//Receive length intLen =0; while(-1! = (len = bis.read (flush))) {//Print to console //system.out.println (New String (Flush,0,len));Bos.write (Flush,0, Len); } bos.flush (); Fileutil.close (bis); } }Catch(FileNotFoundException e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }finally{fileutil.close (BOS); } }/** * File Merging (method two) */ Public void Merge(String DestPath)throwsIOException {///1, create sourceFile dest =NewFile (DestPath);//2, select Stream //sequenceinputstream represents the logical concatenation of other input streams. It starts with an ordered set of input streams, //And starts reading from the first input stream until the end of the file is reached, then reads from the second input stream, and so on, //Until the end of the file containing the last input stream is reached. Sequenceinputstream sis =NULL;//input streamBufferedoutputstream BOS =NULL;//Output source //Create a containerVector<inputstream> VI =NewVector<inputstream> (); for(intI=0; i< This. Blockpath.size (); i++) {Vi.add (NewBufferedinputstream (NewFileInputStream (NewFile ( This. Blockpath.get (i)))); }//sequenceinputstream sis = new Sequenceinputstream (vi.elements ());BOS =NewBufferedoutputstream (NewFileOutputStream (dest,true));//indicates appendSIS =NewSequenceinputstream (Vi.elements ());//Buffer byte[] Flush =New byte[1024x768];//Receive length intLen =0; while(-1! = (len = sis.read (flush))) {//Print to console //system.out.println (New String (Flush,0,len));Bos.write (Flush,0, Len); } bos.flush (); Fileutil.close (SIS); } Public Static void Main(string[] args) {//source file path, size per block (number of bytes), Target fast pathSplitfile split =NewSplitfile ("G:/writer.txt", -,"G:/try");//File initializationSplit.init ();//File Segmentation Try{//split.split ("G:/try");Split.split (Split.destblockpath); }Catch(IOException e) {E.printstacktrace (); }//File Merging Try{Split.merge ("G:/try/test.txt"); }Catch(IOException e) {E.printstacktrace (); }//system.out.println (file.size);}}
File splitting and merging (Java)