JAVA------IO流中对象流的读取问题
我在程序的开始先读取文件里的对象流,发现这样会使我的文件内容清空,求解答import java.io.Serializable;
class Student implements Serializable{//学生类
private static final long serialVersionUID = 1L;
String name="初始化";
String address="初始化";
char sex='男';
int age=18;
float mathScore=100;//数学成绩
float englishScore=100;//英语成绩
void setName(String name) {
this.name=name;
}
void setAddress(String address) {
this.address=address;
}
void setSex(char sex) {
this.sex=sex;
}
void setAge(int age) {
this.age=age;
}
void setMathScore(float mathScore) {
this.mathScore = mathScore;
}
void setEnglishScore(float englishScore) {
this.englishScore = englishScore;
}
String getName() {
return this.name;
}
String getAdress() {
return this.address;
}
char getSex() {
return this.sex;
}
int getAge() {
return this.age;
}
float getMathScore() {
return this.mathScore;
}
float getEnglishScore() {
return this.englishScore;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Student> list = new ArrayList<>();
// File student = new File("E:\\File类", "student.txt");
FileOutputStream outstu = new FileOutputStream("E:\\File类\\\\student.txt");
ObjectOutputStream out = new ObjectOutputStream(outstu);// 对象流输出到文件
FileInputStream instu = new FileInputStream("E:\\File类\\\\student.txt");
ObjectInputStream in = new ObjectInputStream(instu);// 对象流输入到程序
ArrayList<Student> liststu = new ArrayList<>();
try {
@SuppressWarnings("unchecked")
ArrayList<Student> liststu1 = (ArrayList<Student>) in.readObject();// 读取文件内的数组 存到liststu内
liststu = liststu1;
System.out.println("文件内原有学生");
for (int i = 0; i < liststu.size(); i++) {//数组文件内数组含有的学生
System.out.println(liststu.get(i).name + " " + liststu.get(i).address + " " + liststu.get(i).sex + " "
+ liststu.get(i).age + " " + liststu.get(i).mathScore + " " + liststu.get(i).englishScore);
}
} catch (Exception e) {
System.out.println(e);
}
// try {
// TimeUnit.SECONDS.sleep(1);//让程序等待1秒
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
System.out.println("录入学生信息:");
Scanner sc = new Scanner(System.in);
for (;;) {// 录入学生
System.out.println("依次输入姓名、地址、性别、年龄、数学成绩和英语成绩:");
Student stu = new Student();
String name = sc.next();
if (name.charAt(0) == '0') {// 结束录入
break;
}
stu.setName(name);
stu.setAddress(sc.next());
stu.setSex(sc.next().charAt(0));
stu.setAge(sc.nextInt());
stu.setMathScore(sc.nextFloat());
stu.setEnglishScore(sc.nextFloat());
list.add(stu);// 添加到list数组
}
for (int i = 0; i < list.size(); i++) {// 更新名字和地址相同的学生信息
for (int j = 0; j < liststu.size(); j++) {
if (list.get(i).name == liststu.get(j).name && list.get(i).address == liststu.get(j).address) {
liststu.get(j).address = list.get(i).address;
liststu.get(j).sex = list.get(i).sex;
liststu.get(j).age = list.get(i).age;
liststu.get(j).mathScore = list.get(i).mathScore;
liststu.get(j).englishScore = list.get(i).englishScore;
break;
}
}
liststu.add(list.get(i));// 直接添加到尾部
}
// for (int i = 0; i < liststu.size(); i++) {
// out.writeObject(liststu.get(i));// 把录入的跟原有的学生一起写进文件(BUT只能把最后一个学生写入文件)
// }
out.writeObject(liststu);// 把新录入的跟原有的学生 数组 写进文件
try {
System.out.println("文件内当前学生");
while (true) { // 输出文件里数组内的所有学生对象
@SuppressWarnings("unchecked")
ArrayList<Student> stu = (ArrayList<Student>) in.readObject();
for (int i = 0; i < stu.size(); i++) {
System.out.println(stu.get(i).name + " " + stu.get(i).address + " " + stu.get(i).sex + " "
+ stu.get(i).age + " " + stu.get(i).mathScore + " " + stu.get(i).englishScore);
}
}
} catch (Exception e) {
}
in.close();
out.close();
sc.close();
}
}
{:10_312:}
页:
[1]