新人求教关于 文件的读写
就是想练习一下hashtable的使用 最后的代码想要保存在txt中在读出来 但是能写进入 读不出来不知道 为什么还有那个hashcode方法的重载真的不太会会不会是哪里有问题?
谢谢大家 很急用的
package Test;
import *;
import java.util.*;
class Student{
String No, Name, Sex, BirthDate, Member, Address;
FileOutputStream out;
ObjectOutputStream objectOut;
FileInputStream in;
ObjectInputStream objectIn;
public Student(String No, String Name,String Sex, String BirthDate,
String Member, String Address){
this.No = No;
this.Name = Name;
this.Sex = Sex;
this.BirthDate = BirthDate;
this.Member = Member;
this.Address = Address;
}
public String getNo(){
return No;
}
public String getName(){
return Name;
}
public String getSex(){
return Sex;
}
public String getBirthDate(){
return BirthDate;
}
public String getMember(){
return Member;
}
public String getAddress(){
return Address;
}
//重写
public boolean equals(Object o){
if( (o == null) || !(o instanceof Student)){
return false;
} else if(No.equals( ((Student)o).getNo() ) ){
return true;
}
return false;
}
public int hashcode(){
int hash = 5;
hash = 97 * hash +(No != null? No.hashCode() : 0);
return hash;
}
public String toString(){
return (No+" "+Name+" "+Sex+" "+BirthDate+" "+Member+" "+Address);
}
}
public class TestHashTable {
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
Map<String, Student> map = new Hashtable<String,Student>();
Student s1 = new Student("001", "王宇", "男", "1992-12-29", "是", "西安");
Student s2 = new Student("002", "陈诗涵", "女", "1992-04-02", "是", "锦州");
Student s3 = new Student("003", "闫飞", "男", "1992-12-29", "是", "周至");
Student s4 = new Student("004", "李瑶瑶", "女", "1991-12-29", "是", "咸阳");
map.put(s1.getNo(), s1);
map.put(s2.getNo(), s2);
map.put(s3.getNo(), s3);
map.put(s4.getNo(), s4);
//遍历hashtable
for( Iterator it = map.keySet().iterator(); it.hasNext();){
String key = (String)it.next();
System.out.println(map.get(key));
}
System.out.println("********************************");
if(map.containsKey("王宇")){
System.out.println(map.get("wangyu"));
}
System.out.println("********************************");
Student s5 = new Student("005", "程龙", "男", "1991-12-29", "是", "西安");
map.put("005", s5);
//遍历hashtable
for( Iterator it = map.keySet().iterator(); it.hasNext();){
String key = (String)it.next();
System.out.println(map.get(key));
}
System.out.println("********************************");
map.remove("005");
for( Iterator it = map.keySet().iterator(); it.hasNext();){
String key = (String)it.next();
System.out.println(map.get(key));
}
System.out.println("********************************");
File file = new File("D://testInfo.txt");
if(!file.exists()){
try{
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream objectOut = new ObjectOutputStream(out);
objectOut.writeObject(map);
objectOut.close();
out.close();
} catch(IOException e){
}
}
System.out.println("********************************");
System.out.println("***************写入完成*************");
Map<String, Student> hash = new Hashtable<String,Student>();
try{
FileInputStream in = new FileInputStream(file);
ObjectInputStream objectIn = new ObjectInputStream(in);
hash = (Hashtable)objectIn.readObject();
objectIn.close();
in.close();
} catch(IOException e){
}
if(hash.containsKey("001")){
System.out.println("yes");
} else System.out.println("no");
// for( Iterator it1 = hash.keySet().iterator(); it1.hasNext();){
// String key = (String)it1.next();
// System.out.println(hash.get(key));
// }
System.out.println("*************读取完成***************");
}
}