第一题:实现文件拆分、合并。 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; } }
大数据Java基础第十五天作业
原创
©著作权归作者所有:来自51CTO博客作者森林敏的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:大数据Java基础第十四天作业
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
学习第十五天
好好学
i++ #include #define