如何在java中从二进制文件中写入和读取student对象
我正在编写一个程序来存储用户在二进制文件中创建的一些学生对象。 该程序还可以从创建的二进制文件中读取数据。 我认为将数据写入二进制文件有问题 这是一个存储2名学生的示例程序可以存储学生,但在读取这些信息时存在问题(我不知道在编写数据时也可能存在问题)。 有什么建议可以让代码更好,运行时不会出错吗 还有没有更好的方法来编写这个程序来存储用户想要的学生数量,并读取数据并进行搜索?? 我的意思是有一些搜索选项,用户可以搜索特定的学生 这是我的密码:如何在java中从二进制文件中写入和读取student对象,java,Java,我正在编写一个程序来存储用户在二进制文件中创建的一些学生对象。 该程序还可以从创建的二进制文件中读取数据。 我认为将数据写入二进制文件有问题 这是一个存储2名学生的示例程序可以存储学生,但在读取这些信息时存在问题(我不知道在编写数据时也可能存在问题)。 有什么建议可以让代码更好,运行时不会出错吗 还有没有更好的方法来编写这个程序来存储用户想要的学生数量,并读取数据并进行搜索?? 我的意思是有一些搜索选项,用户可以搜索特定的学生 这是我的密码: import java.util.*; pu
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;
}```
import java.util.*;
公共类将对象写入二进制文件{
公共静态void main(字符串[]args)抛出FileNotFoundException、ClassNotFoundException、IOException{
扫描仪输入=新扫描仪(System.in);
对于(int i=0;i<2;i++){
System.out.println(“学生名:”);
字符串fName=input.next();
System.out.println(“学生姓氏:”);
字符串lName=input.next();
System.out.println(“输入ID:”);
int id=input.nextInt();
System.out.println(“输入gpa:”);
字节gpa=input.nextByte();
写入数据(fName、lName、id、gpa);
}
readData();
}
公共静态void writedata(字符串fName、字符串lName、int-id、字节gpa){
学生stu=新学生();
stu.first=fName;
stu.last=lName;
stu.id=id;
stu.gpa=gpa;
试一试{
ObjectOutputStream oos=新的ObjectOutputStream(新的FileOutputStream(“object.dat”));
System.out.println(“写入信息”);
oos.writeObject(stu);
oos.close();
系统输出打印项次(“完成”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public static void readData()抛出FileNotFoundException、IOException、ClassNotFoundException{
ObjectInputStream ois=新ObjectInputStream(新文件InputStream(“object.dat”));
对于(int i=0;i<2;i++){
System.out.println(“读取对象”);
student stu=(student)ois.readObject();
系统输出打印(stu.first);
系统输出打印项次(最后一次);
系统输出打印号(stu.id);
系统输出打印项次(stu.gpa);
System.out.println(“完成读取对象”);
}
ois.close();
}
}
学生实现可序列化的类{
先串;
最后一串;
int-id;
字节gpa;
}```
当您使用ObjectOutputStream
写入多个对象时,它会生成一个块数据记录,用于存储有关对象的信息。如果在任何时候关闭流,然后重新打开流以写入更多对象,则块数据记录将被覆盖。每次调用writedata
时,都会出现此问题。您也不会在追加模式下打开文件,因此它只是替换现有数据,但这仍然会在此处导致问题
但是有一种更好的方法可以解决这个问题,并让您将代码推广到许多学生
。继续创建对象,然后将它们全部存储在列表中,只需将单个列表
对象写入文件即可。序列化过程也会将列表的所有成员写入文件
您应该将类名大写,但在这里我将坚持使用您的类名:
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);
}
List studentList=new ArrayList();
对于(int i=0;i
上面的代码将在每次运行时覆盖现有文件,但我将此留给您。您的代码可能还有其他问题。Wow!谢谢你的帮助,我真的很感激!我从未想过将所有对象存储在arraylist中,然后将其写入文件。非常感谢:)