How can we write objects of 10 students into a data file in Java Programming Language?

Java

1 answer

Answer

1168462

2026-04-01 04:50

+ Follow

Java
Java

To write objects of 10 students into a data file in Java, you can use ObjectOutputStream in combination with FileOutputStream. First, create a Student class that implements Serializable. Then, open a FileOutputStream to the desired file, wrap it with ObjectOutputStream, and use a loop to write each student object to the file. Finally, close the output stream to ensure all data is flushed and resources are released. Here's a simple example:

<code class="language-Java">FileOutputStream fileOut = new FileOutputStream("students.dat");

ObjectOutputStream out = new ObjectOutputStream(fileOut); for (int i = 0; i < 10; i++) { out.writeObject(new Student(/* parameters */)); } out.close();

</code>
ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.