日期:2014-05-20  浏览次数:20831 次

java作业-通讯录,无数据库,写入数据时出现乱码
[code]import java.util.*;
import java.io.*;
import java.lang.*;

/**
 *
 * @author Administrator
 */
 
public class Person implements Serializable{
  private String name;
  private int age;
  private long phone;
  private long id;
  Person(String name,int age,long phone,long id){
  this.name=name;
  this.age=age;
  this.phone=phone;
  this.id=id;
  }
  public String getName(){
  return name;
  }
  public void setName(String name){
  this.name=name;
  }
  public int getAge(){
  return age;
  }
  public void setAge(int age){
  this.age=age;
  }
  public long getPhone(){
  return phone;
  }
  public void setName(long phone){
  this.phone=phone;
  }
  public long getId(){
  return id;
  }
  public void setId(long id){
  this.id=id;
  }
public static void main(String[] args) {
  Map map = new HashMap();
  exit:
  try{
  FileOutputStream fos=new FileOutputStream("C:\\Person.txt");
  ObjectOutputStream obs=new ObjectOutputStream(fos);
  obs.writeObject(map);
  for(;;)
  {
  System.out.println("1、保存 2、查询3、删除 4、修改 5、退出");
  Scanner scanner1 = new Scanner(System.in);//扫描器,接受控制台的输入信息
  String str=scanner1.nextLine();//取出控制台的一行信息,也就是你输入的信息
  char[] choose=str.toCharArray();//把取道的字符串变成一个char数组
  FileInputStream fis=new FileInputStream("C:\\Person.txt");
  ObjectInputStream ois=new ObjectInputStream(fis);
  ois.readObject();
  if(choose.length>1)
  {
  System.out.println("只能输入1-5的数字");
  }else {
  switch (choose[0]) {
  case '1':
  FileInputStream fix=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oix=new ObjectInputStream(fix);
  map=(HashMap)oix.readObject();
  System.out.print("请输入学号:");
  Scanner scanner2 = new Scanner(System.in);
  String id = scanner2.nextLine();
  System.out.print("请输入姓名:");
  Scanner scanner3 = new Scanner(System.in);
  String name = scanner3.nextLine();
  System.out.print("请输入电话号码:");
  Scanner scanner4 = new Scanner(System.in);
  String phone = scanner4.nextLine();
  System.out.print("请输入年龄:");
  Scanner scanner5 = new Scanner(System.in);
  String age= scanner5.nextLine();
 
  map.put(id,map);
  System.out.println("保存成功");
  break;
  case '2':
  FileInputStream fib=new FileInputStream("C:\\Person.txt");
  ObjectInputStream oib=new ObjectInputStream(fib);
  oib.readObject();
  if(map.size()==0)
  {
  System.out.println("通讯录为空,请先添加");
  }else {
  Iterator it = map.keySet().iterator();//map.keySet()返回一个对象实例,调用iterator()方法,返回Iterator类型赋给it。实现遍历输出
  while(it.hasNext()){
  String key = (String) it.next();
  String value = (String) map.get(key);
  System.out.println(key+" "+value);
  }
  }
  break;
  case '3':