连续序列化一个类型的两个对象到文件;怎么反序列读文件时只读出后面个输入的对象?请看下面程序:
连续输入两个StudentMessage的对象:张三,20;李四21。并序列化到"f:\\txt\\学员表.txt"文件里。packagechs;importjava.u...
连续输入两个StudentMessage的对象:张三,20 ; 李四 21。并序列化到"f:\\txt\\学员表.txt"文件里。
package chs;
import java.util.Scanner;
import java.io.*;
class StudentMessage{
private String name;
private int age;
StudentMessage(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+name+",年龄 "+age;
}
}
public class StudentMessageDemo{
public static void main(String[]args){
String nameTemp;
int ageTemp;
String flag;
int num;
File a=new File("F:\\txt\\学员表.txt");
a.createNewFile();
Scanner rd=new Scanner(System.in);
try{
while(true){
System.out.println("请选择菜单键: 1(增添学员信息), 2(退出程
序)");
num=rd.nextInt();
switch(num){
case 1:
System.out.println("请输入学员姓名");
nameTemp=rd.next();
System.out.println("请输入学员年龄");
ageTemp=rd.nextInt();
StudentMessage dx1=new StudentMessage(nameTemp,ageTemp);
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream(a));
obs.writeObject(dx1);
obs.close();
break;
case 2:
return;
default:
System.out.println("请输入正确的标号:");
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
反序列读出对象如下:
package chs;
import java.io.*;
public class StudentOut{
public static void main(String[]args){
try{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("f:\\txt\\学员表.txt"));
StudentMessage a=(StudentMessage)ois.readObject();
System.out.println(a);
ois.close();
}
catch(Exception e){
}
}
}
结果总是只能显示后面个输入的对象:李四,21。请大侠们给我解释下这是怎么回事,为什么不是像这样(张三,20 李四,21)输出两个对象呢? 展开
package chs;
import java.util.Scanner;
import java.io.*;
class StudentMessage{
private String name;
private int age;
StudentMessage(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名:"+name+",年龄 "+age;
}
}
public class StudentMessageDemo{
public static void main(String[]args){
String nameTemp;
int ageTemp;
String flag;
int num;
File a=new File("F:\\txt\\学员表.txt");
a.createNewFile();
Scanner rd=new Scanner(System.in);
try{
while(true){
System.out.println("请选择菜单键: 1(增添学员信息), 2(退出程
序)");
num=rd.nextInt();
switch(num){
case 1:
System.out.println("请输入学员姓名");
nameTemp=rd.next();
System.out.println("请输入学员年龄");
ageTemp=rd.nextInt();
StudentMessage dx1=new StudentMessage(nameTemp,ageTemp);
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream(a));
obs.writeObject(dx1);
obs.close();
break;
case 2:
return;
default:
System.out.println("请输入正确的标号:");
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
反序列读出对象如下:
package chs;
import java.io.*;
public class StudentOut{
public static void main(String[]args){
try{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("f:\\txt\\学员表.txt"));
StudentMessage a=(StudentMessage)ois.readObject();
System.out.println(a);
ois.close();
}
catch(Exception e){
}
}
}
结果总是只能显示后面个输入的对象:李四,21。请大侠们给我解释下这是怎么回事,为什么不是像这样(张三,20 李四,21)输出两个对象呢? 展开
展开全部
想了一下 应该不是那个问题 我给你的代码修改下 这个就像楼上的说的被覆盖了:
序列化的:
package chs;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentMessageDemo {
public static void main(String[] args) throws IOException {
String nameTemp;
int ageTemp;
String flag;
int num;
ArrayList<StudentMessage> student =new ArrayList<StudentMessage>();
File a = new File("F:\\txt\\学员表.txt");
a.createNewFile();
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream(a));
Scanner rd = new Scanner(System.in);
while (true) {
System.out.println("请选择菜单键: 1(增添学员信息), 2(退出程序)");
num = rd.nextInt();
switch (num) {
case 1:
System.out.println("请输入学员姓名");
nameTemp = rd.next();
System.out.println("请输入学员年龄");
ageTemp = rd.nextInt();
StudentMessage dx1 = new StudentMessage(nameTemp, ageTemp);
student.add(dx1);
break;
case 2:
obs.writeObject(student);
obs.close();
return;
default:
System.out.println("请输入正确的标号:");
}
}
}
}
反序列化的:
package chs;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class StudentOut {
public static void main(String[] args) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"f:\\txt\\学员表.txt"));
ArrayList<StudentMessage> studentMessages=new ArrayList<StudentMessage>();
studentMessages= (ArrayList<StudentMessage>) ois.readObject();
for(StudentMessage student:studentMessages){
System.out.println(student.toString());
}
ois.close();
} catch (Exception e) {
}
}
}
序列化的:
package chs;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentMessageDemo {
public static void main(String[] args) throws IOException {
String nameTemp;
int ageTemp;
String flag;
int num;
ArrayList<StudentMessage> student =new ArrayList<StudentMessage>();
File a = new File("F:\\txt\\学员表.txt");
a.createNewFile();
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream(a));
Scanner rd = new Scanner(System.in);
while (true) {
System.out.println("请选择菜单键: 1(增添学员信息), 2(退出程序)");
num = rd.nextInt();
switch (num) {
case 1:
System.out.println("请输入学员姓名");
nameTemp = rd.next();
System.out.println("请输入学员年龄");
ageTemp = rd.nextInt();
StudentMessage dx1 = new StudentMessage(nameTemp, ageTemp);
student.add(dx1);
break;
case 2:
obs.writeObject(student);
obs.close();
return;
default:
System.out.println("请输入正确的标号:");
}
}
}
}
反序列化的:
package chs;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class StudentOut {
public static void main(String[] args) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"f:\\txt\\学员表.txt"));
ArrayList<StudentMessage> studentMessages=new ArrayList<StudentMessage>();
studentMessages= (ArrayList<StudentMessage>) ois.readObject();
for(StudentMessage student:studentMessages){
System.out.println(student.toString());
}
ois.close();
} catch (Exception e) {
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询