我正在编写一个程序来将用户创建的一些学生对象存储在二进制文件中。该程序还可以从创建的二进制文件中读取数据。我认为将数据写入二进制文件存在问题。
这是一个存储2个学生的示例,程序可以很好地存储学生,但是读取该信息存在问题(我不知道写入数据时也可能有问题)。有什么建议可以使代码更好并且可以正确运行吗?
还有什么更好的方法可以编写此程序来存储用户想要的尽可能多的学生,并读取和搜索数据?我的意思是拥有某种搜索选项,用户可以搜索特定的学生。
这是我的代码:
import java.util.*;
public class write_object_to_binary_file {
public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException {
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.println("Student first name: ");
String fName = input.next();
System.out.println("Student last name: ");
String lName = input.next();
System.out.println("Enter ID: ");
int id = input.nextInt();
System.out.println("Enter gpa: ");
byte gpa = input.nextByte();
writedata(fName, lName, id, gpa);
}
readData();
}
public static void writedata(String fName, String lName, int id, byte gpa) {
student stu = new student();
stu.first = fName;
stu.last = lName;
stu.id = id;
stu.gpa = gpa;
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
System.out.println("Writing information");
oos.writeObject(stu);
oos.close();
System.out.println("Done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readData() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat"));
for (int i = 0; i < 2; i++) {
System.out.println("reading object");
student stu = (student) ois.readObject();
System.out.println(stu.first);
System.out.println(stu.last);
System.out.println(stu.id);
System.out.println(stu.gpa);
System.out.println("done reading object");
}
ois.close();
}
}
class student implements Serializable {
String first;
String last;
int id;
byte gpa;
}```
当您使用编写多个对象时ObjectOutputStream
,它将创建一个块数据记录,其中存储了有关对象的信息。如果您随后在任何时候关闭流,然后重新打开它以写入更多对象,则块数据记录将被覆盖。每次调用writedata
此问题都会发生。您也不会以追加模式打开文件,因此它只是替换现有数据,但这仍然会在这里引起问题。
但是,有一种更好的方法可以解决此问题,并使您可以将代码泛化为许多代码students
。继续制作您的对象,然后将它们全部存储在中List
,然后将单个List
对象写入文件。序列化过程也会将列表的所有成员也写入文件。
您应该使用大写的类名,但是我会在这里坚持使用您的类名:
List<student> studentList = new ArrayList<>();
for (int i = 0; i < n; ++i) {
System.out.println("Student first name: ");
String fName = input.next();
System.out.println("Student last name: ");
String lName = input.next();
System.out.println("Enter ID: ");
int id = input.nextInt();
System.out.println("Enter gpa: ");
byte gpa = input.nextByte();
student stu = new student();
//setting these fields with a constructor to would be better
stu.first = fName;
stu.last = lName;
stu.id = id;
stu.gpa = gpa;
studentList.add(stu);
}
//this part is serializing the list and writing it to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
System.out.println("Writing information");
oos.writeObject(studentList);
oos.close();
System.out.println("Done");
//this part deserializes the list from file, and then iterates over its members
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat"));
System.out.println("reading object");
List<student> list = (List) ois.readObject();
System.out.println("done reading object");
for (student stu : list) {
System.out.println(stu.first);
System.out.println(stu.last);
System.out.println(stu.id);
System.out.println(stu.gpa);
}
上面的代码将在每次运行时覆盖现有文件,但我将其留给您。您的代码也可能有其他问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句