我写了一个通讯录,我写用文件PhoneBook.txt来保存记录,但是我每次重新运行程序的时候数据(又会丢失)应该说我的程序读取不到对象,每次运行输出时回到原来的状况,不知道那里出问题了,请指出问题和修改下~说明下为什么!~
先谢先!~
程序如下:
////////TestCallNote
import java.io.*;
import java.util.*;
public class TestCallNote { 
  static HashMap<String,String> hashmap=new HashMap<String,String>();
  static Scanner in=new Scanner(System.in);
  
  public static void main(String[] args) {
   
   File file=new File("PhoneBook.txt");
   TestCallNote note=new TestCallNote(file);
   note.saved(file);
      while(true){
       System.out.println("输入空为显示全部记录,输入姓名则查找姓名,输入电话号码则查找姓名,输入一个以上的记录则添加记录");
       System.out.println("输入exit则退出程序");
       note.started(); 
       note.saved(file);
      }
  }
  //打印记录
  public void printElement(HashMap hm){
   Set keys=hm.keySet();
   Iterator it=keys.iterator();
   while(it.hasNext()){
    Object ok=it.next();
    System.out.println("姓名: "+ok+" 号码: "+hm.get(ok));
   }
  }
  //增加记录
  public void addElement(CallNote note){
   hashmap.put(note.getName(), note.getNumber());
  }
  
     //(用主键找值)姓名找号码
  public void printElement(HashMap hm,String o){
   Set keys=hm.keySet();
   Iterator it=keys.iterator();
      while(it.hasNext()){
       Object ok=it.next();
       Object ov=hm.get(ok);
       if(ok.equals(o))
        System.out.println(ov);
      }
  }
//  (用值找主键组)号码找姓名
  public void searchKeys(HashMap hm,String value){
   ArrayList al=new ArrayList();
   Set keys=hm.keySet();
   Iterator it=keys.iterator();
   while(it.hasNext()){
    Object ok=it.next();
    Object ov=hm.get(ok);
    if(ov.equals(value)){
     al.add(ok);
    }
   }
   if(al.size()!=0){
    System.out.println(al);
   }
  }
  //检查电话的合法性
  public boolean isNumber(String str){
   if(str.length()!=11){
    System.out.println("这不是手机号码,手机号码应该为11位数字~");
    return false;
   }
   try{
    long l=Long.parseLong(str);
   }catch(Exception e){
    System.out.println("这不是手机号码,手机号码应该为11位数字~");
    return false;
   }
   return true;
  }
  //初始化开始
  public void started(){
   addElement(new CallNote("黄林权","13802439104"));
   addElement(new CallNote("王键宁","13725487564"));
   addElement(new CallNote("初楷","13325478216"));
   addElement(new CallNote("恒毅","13125478562"));
   addElement(new CallNote("雄辉","13505485415"));
   try{  
    String start=in.nextLine();
    if(start.equals("exit")){
     System.exit(0);  //输入exit则退出程序
    }
       if(start.equals("")) printElement(hashmap);  //输入空为显示全部记录
       
       if(!(start.equals(""))){//输入姓名则查找姓名,输入电话号码则查找姓名,输入一个以上的记录则添加记录
        printElement(hashmap,start);
        searchKeys(hashmap,start);
        String[] str=start.split(" ");
        if(str.length>1){
         if(isNumber(str[1])){
          addElement(new CallNote(str[0],str[1]));
          printElement(hashmap);
         }    
        }
       }
   }catch(Exception e){
    e.printStackTrace();
   }
  }
  //用输出流把记录保存在文件上
  public void saved(File file){
   try{
  
    FileOutputStream fos=new FileOutputStream(file,true);//这里是追加内容
    ObjectOutputStream out=new ObjectOutputStream(fos);
    out.writeObject(hashmap);
    
    out.close();
   }catch(Exception e){
    e.printStackTrace();
    System.exit(1);
   }
  }
  //用构造函数来初始化输入流
  public TestCallNote(File file){
   try{
    FileInputStream fis=new FileInputStream(file);
    ObjectInputStream in=new ObjectInputStream(fis);
    hashmap=(HashMap)in.readObject();
    in.close();
   }catch(Exception e){
    e.printStackTrace();
   }
  }
}
/////CallNote.java
import java.io.*;
import java.util.Scanner;
public class CallNote implements Serializable{
    private String name;
    private String number;
    static Scanner in=new Scanner(System.in);
    public CallNote(String name,String number){
        this.name=name;
        this.number=number;
    }
    public String getName(){
        return name;
    }
    public String getNumber(){
        return number;
    }
    
    public String toString(){
        return "name="+name+" number="+number;
    }
    public int hashCode(){
        return name.hashCode()*number.hashCode();
    }
    public boolean equals(Object o){
        if(o==null) return false;
        if(this==o)return true;
        if(!(o instanceof CallNote)) return false;
        CallNote c=(CallNote)o;
        return this.name.equals(c.name) && (this.number==c.number);
    }
}
[此贴子已经被作者于2007-5-5 10:43:13编辑过]



 
											





 
	    

 
	



