import java.io.*;
importjava.util.*;
class StudentRecordimplements Serializable //实现此接口的子类可序列化
{
private static final long serialVersionUID =122788087250556887L;
String name;
String birthday;
int age;
String day;
String phoneNumber;
//构造方法初始化StudentRecord类数据成员
public StudentRecord(String name,int age,
String birthday,String phoneNumber,String day)
{
this.name=name;
this.age=age;
this.birthday=birthday;
this.phoneNumber=phoneNumber;
this.day=day;
}
void Display()
{
System.out.println(name+"\t"+age+"\t"+birthday+"\t"+phoneNumber+"\t"+day);
}
}
classStudentAccount //StudentAccount类中定义Vector向量类存储数据
{
int num;
Vector<StudentRecord> rec;
String filename="Record.dat";
public StudentAccount()
{
rec=newVector<StudentRecord>();
Input();
Display();
}
void Input() //Input方法用于写入数据流到文件,记录数据内容
{
try
{
FileInputStream file=new FileInputStream(filename);
ObjectInputStream in=new ObjectInputStream(file);
rec.setSize(100);
rec=(Vector<StudentRecord>)in.readObject();//由一个类型转换的警告提示怎么消除??
num=rec.size();
file.close();
}
catch(Exception e)
{
}
}
void Save() //Save()方法用于保存输入的数据到指定文件
{
try
{
FileOutputStream fout=new FileOutputStream(filename);
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(rec);
out.flush();
fout.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
void Display()
{
System.out.println("姓名\t年龄\t生日\t\t手机\t\t日期");
StudentRecord srec;
for(int i=0;i<num;i++)
{
srec=(StudentRecord)rec.get(i);
srec.Display();
}
}
void Begin() //Begin()方法中Scanner类用于扫描输入内容并记录到向量类中
{
Scanner in=new Scanner(System.in);
String continu="y";
Calendar cal=Calendar.getInstance();
String day=cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DATE)+"日";
while (continu.equalsIgnoreCase("y"))
{
String namerecord="";
int agerecord;
String birthdayrecord;
String phonenumberrecord;
System.out.println("请输入姓名:");
namerecord=in.next();
System.out.println("请输入年龄:");
agerecord=in.nextInt();
System.out.println("请输入生日:");
birthdayrecord=in.next();
System.out.println("请输入手机号码:");
phonenumberrecord=in.next();
StudentRecord sr=newStudentRecord(namerecord,agerecord,birthdayrecord,
phonenumberrecord,day);
rec.addElement(sr);
System.out.println("还要继续吗(y/n)");
continu =in.next();
}
Save();
}
}
public classInformationRecord
{
public static void main(String[] args)
{
StudentAccount rec=new StudentAccount();
rec.Begin();
}
}
红色字体标注的地方由一个警告请问怎么解决??谢谢rec=(Vector<StudentRecord>)in.readObject();//由一个类型转换的警告提示怎么消除??
|
|