Big Data Java Foundation 15th day job

Source: Internet
Author: User

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

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.