[英]how to write and read student object from a binary files in java
我正在編寫一個程序來存儲用戶在二進制文件中創建的一些學生 object。 該程序還可以從創建的二進制文件中讀取數據。 我認為將該數據寫入二進制文件時存在問題。
這是一個存儲 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
編寫多個對象時,它會創建一個塊數據記錄來存儲有關對象的信息。 如果您在任何時候關閉 stream 並稍后重新打開它以寫入更多對象,則塊數據記錄將被覆蓋。 每次調用writedata
時都會出現此問題。 您也沒有在 append 模式下打開文件,因此它只是替換現有數據,但這仍然會導致問題。
但是有一種更好的方法可以解決這個問題,並且可以讓您將您的代碼推廣到許多students
。 繼續制作您的對象,然后將它們全部存儲在List
中,然后將單個List
object 寫入文件。 序列化過程也會將列表的所有成員寫入文件。
您應該大寫類的名稱,但我會在這里堅持使用您的 class 名稱:
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);
}
上面的代碼每次運行時都會覆蓋現有文件,但我把它留給你。 您的代碼可能還有其他問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.