大数据Java基础第十五天作业
2016-06-06 23:26
351 查看
第一题:实现文件拆分、合并。 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(); } } 第二题:使用串行化技术实现对象图的深度复制?100只Cat集合,Cat的owner不需要串行化。 import java.util.List; import java.util.ArrayList; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.Serializable; class SerializeDemo{ public static void main(String[] args) throws Exception{ List<Cat> cat_list = new ArrayList<Cat>(); for(int i=0;i<100;i++){ cat_list.add(new Cat("cat_name" + i,170,23,new Person("owner"+i))); } ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e:/tmp.dat")); oos.writeObject(cat_list); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/tmp.dat")); cat_list = (List<Cat>)ois.readObject(); ois.close(); for(Cat cat : cat_list){ System.out.println("name:" + cat.getName() + ",height:" + cat.getHeight() + ",age:" + cat.getAge() + ",pserson:" + cat.getOwnerName()); } } } class Cat implements Serializable{ private String name; private int height; private int age; private Person owner;//transient 出现java.lang.NullPointerException public Cat(String name,int height,int age,Person owner){ this.name = name; this.height = height; this.age = age; this.owner = owner; } public String getName(){ return name; } public int getHeight(){ return height; } public int getAge(){ return age; } public String getOwnerName(){ return owner.getName(); } } class Person implements Serializable{ private String name; public Person(String name){ this.name = name; } public String getName(){ return name; } }
本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1786761
相关文章推荐
- 开源大数据查询分析引擎现状
- [Leetcode]70. Climbing Stairs
- [Leetcode]172. Factorial Trailing Zeroes
- [Leetcode]217. Contains Duplicate
- Neo4j索引笔记之SchemaIndex和LegacyIndex
- 193_AIDL的使用
- Grails用CONSOLE测试,比写集成测试还快
- vector中pair的排序方法
- 196. Delete Duplicate Emails
- [转载] fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
- Rails中force index
- TIME_WAIT状态存在的必要性
- Ex2010-17 Linked Mailbox in Exchange Server
- ITIL发展的云计算之路
- __stack_chk_fail栈检查失败
- return columns.All(new Func<string, bool>(list.Contains));
- 大数据服务脚本化管理
- Ex2010-16 Connect Users On Child Domain to Exchange On Root Domain
- scala之trait详解三:延时绑定
- Exchange 2010 PS 之new-mailboxDatabase&Get-MailboxDatabase&Move-databasePath!