您的位置:首页 > 大数据

大数据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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: