Serializable的使用如何实现在控制台,新增,查询实例化的对象
import *;class mode implements Serializable
{
//static final long serialVersionUID=123456L;
static String m_name;
String m_id;//学号
String m_phonenumber;
String m_sex;
public mode(String name,String id,String phonenumber,String isman)
{
m_name=name;
m_id=id;
m_phonenumber=phonenumber;
m_sex=isman;
}//构造函数结束
public static String mb_getname()
{
return m_name;
}
public void mb_output()
{
System.out.println("姓名:"+m_name);
System.out.println("学号:"+m_id);
System.out.println("手机号码:"+m_phonenumber);
System.out.println("性别:"+m_sex);
}//mb_output结束
}//类mode结束
public class shiyan3
{
static String m_name;
String m_id;//学号
String m_phonenumber;
String m_sex;
public static void mb_printlnInfo()
{
System.out.println("查询还是新增???");
System.out.println("\t 0 :退出; 1 : 新增 ;2 :查询");
}
public static int mb_getInt(BufferedReader f)
{
try
{
String s=f.readLine();
int i=Integer.parseInt(s);
return i;
}
catch(Exception e)
{
return -1;
}//try_catch
}//mb_get....
public static String mb_getname()
{
return m_name;
}
public void mb_setname(String name)
{
m_name=name;
}
public void mb_setid(String id)
{
m_id=id;
}
public void mb_setphonenumber(String phonenumber)
{
m_phonenumber=phonenumber;
}
public void mb_setsex(String sex)
{
m_sex=sex;
}
public static void main(String[] args)
{
int i; [size=7][size=6][size=7]//得问老师,我没有初始化啊,咋不提示错误啊???不是说局部变量必须初始化吗??[/size][/size][/size]
try
{
BufferedReader f=new BufferedReader(new InputStreamReader(System.in));
do
{
mb_printlnInfo();
i=mb_getInt(f);
if(i==0)
break;
else if(i==1)
{
try
{
System.out.println("请输入姓名:");
BufferedReader fname=new BufferedReader(new InputStreamReader(System.in));
String name=fname.readLine();
System.out.println("您输入了姓名:\n"+name+"\n\n\n请输入学号:");
BufferedReader fid=new BufferedReader(new InputStreamReader(System.in));
String id=fid.readLine();
System.out.println("您输入了学号:"+id+"\n\n\n请输入手机号码:");
BufferedReader fphonenumber=new BufferedReader(new InputStreamReader(System.in));
String phonenumber=fphonenumber.readLine();
System.out.println("你输入了手机号:"+phonenumber+"\n\n\n请输入性别:");
BufferedReader fsex=new BufferedReader(new InputStreamReader(System.in));
String sex=fsex.readLine();
// System.out.println("您输入了"+ex);
System.out.println("您对应为"+name+"的学生信息录入完毕");
System.out.println("\n");
ObjectOutputStream f1=new ObjectOutputStream(new FileOutputStream("tongxun.dat"));
mode s=new mode(name,id,phonenumber,sex);
// mode s=new mode("张三","0708114","13673508502","男");
// mode s1=new mode("李四","0708114","110120119","女");
f1.writeObject(s);
f1.close();
}
catch(Exception e)
{
System.out.println("异常发生:"+e);
e.printStackTrace();
}//try-catch结束
}
else if(i==2)
{
try
{
System.out.println("请输入要查找的姓名:");
BufferedReader fname2=new BufferedReader(new InputStreamReader(System.in));
String name2=(String) fname2.readLine();
ObjectInputStream f2=new ObjectInputStream(new FileInputStream("tongxun.dat"));
mode s=(mode)(f2.readObject());
// if (s.mb_getname()==name2)
// mode s1=(mode)(f2.readObject());
s.mb_output();
// else System.out.println("无相关数据!!退出查找请输入:0\n请重新输入");
f2.close();
}
catch(Exception e)
{
System.out.println("发生异常:"+e);
e.printStackTrace();
}//try-catch结束
}//if else结束
}while(true);//do while 结束
f.close();
}
catch(Exception e)
{
System.out.println("发生异常:"+e);
e.printStackTrace();
}//try -catch 结束
}
}
在下写的源码如上,在控制台已经实现了,新增,就是在查询的时候老是出问题!
请教高人指点!!!!