The first question: Implement file splitting, merging. Import java.io.file;import java.io.randomaccessfile;import java.io.fileoutputstream;import java.io.fileinputstream;import java.io.bufferedoutputstream;import java.io.bufferedinputstream ;import java.util.list;import java.util.arraylist;class randomdome{ Public static void main (String[] args) throws Exception{ randomsplit ("E:/index.txt", 3,5); list <String> inpath_list = new ArrayList<String> (); inpath_list.add ("E:/index0.txt"); inpath_ List.add ("E:/index1.txt"); inpath_list.add ("E:/index2.txt"); randommerge ("E:/index3.txt", Inpath_list); } public static void randomsplit (String inpath,string outpath,int num , Int buf_length) throws Exception{ File File = new file (Inpath); randomaccessfile Raf = new randomaccessfile (file, "R"); long File_length = file.length (); int avg_length = (int) file_length / 3; for (int i=0;i <num;i++) { int start_index = i * avg_length; Int end_index = 0; if (i == (num - 1)) { end_ index = (int) file_length - 1; }else{ end_index = (i + 1) * avg_length - 1; } raf.seek (Start_index); Bufferedoutputstream out = new bufferedoutputstream (New fileoutputstream (Outpath + i)); byte[] buf = new byte[buf_length]; System.out.println (start_index + "---" + end_index); while (True) { int current_index = (int) raf.getfilepointer (); int remain = end_index - current_index + 1; system.out.println (current_index + "---" + remain); if (remain >= Buf.length) { raf.read (BUF); &Nbsp; out.write (BUF); }else{ raf.read (Buf,0,remain); out.write (Buf,0,remain); } if (Raf.getfilepointer () > end_ Index) { break; } } &nbsP; out.close (); } raf.close (); } Public static void randommerge (string outpath,list<string> inpath_list) throws exception{ bufferedoutputstream out = new bufferedoutputstream (New fileoutputstream (outpath)); for (string inpath : inpath_list) { bufferedinputstream in = new bufferedinputstream (new FileInputStream (Inpath)); byte[] buf = new byte[1024]; int len; while ((Len = in.read (BUF)) != -1) { out.write (BUF, 0,len); } in.close (); } out.close (); }}
This article is from the "Forest Sensitive" blog, please be sure to keep this source http://senlinmin.blog.51cto.com/6400386/1786761
Big Data Java Foundation 15th day job