大数据Java基础第十五天作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据Java基础第十五天作业相关的知识,希望对你有一定的参考价值。
第一题:实现文件拆分、合并。 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); out.write(buf); }else{ raf.read(buf,0,remain); out.write(buf,0,remain); } if(raf.getFilePointer() > end_index){ break; } } 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(); } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1786761
以上是关于大数据Java基础第十五天作业的主要内容,如果未能解决你的问题,请参考以下文章