public class Student implements WritableComparable {
private Text name = new Text();
private IntWritable age = new IntWritable();
private Text sex = new Text();
public Student() {
}
public Student(String name, int age, String sex) {
super();
this.name = new Text(name);
this.age = new IntWritable(age);
this.sex = new Text(sex);
}
//set 和get方法省略
public void readFields(DataInput in) throws IOException {
name.readFields(in);
age.readFields(in);
sex.readFields(in);
}
public void write(DataOutput out) throws IOException {
name.write(out);
age.write(out);
sex.write(out);
}
public int compareTo(Object o) {
Student s = (Student) o;
int result = 0;
if ((result = name.compareTo(s.getName())) != 0)
return result;
if ((result = age.compareTo(s.getAge())) != 0)
return result;
if ((result = sex.compareTo(s.getSex())) != 0)
return result;
return 0;
}
}
2 序列化对象的使用(对象写到文件中和从文件中直接读取对象)
public class Client {
public static void main(String[] args) throws IOException {
Student s = new Student("123", 20, "网站");// 从此开始序列化
FileOutputStream fout = new FileOutputStream(new File(
"F:\\testWritable.txt"));
DataOutputStream out = new DataOutputStream(fout);
s.write(out);
fout.close();
out.close();
Student s1 = new Student(); // 从此开始是反序列化
FileInputStream fin = new FileInputStream(new File(
"F:\\testWritable.txt"));
DataInputStream in = new DataInputStream(fin);
s1.readFields(in);
System.out.println("name = " + s1.getName() + ",age = " + s1.getAge()
+ ",sex =" + s1.getSex());
}
}