Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to write this to a binary file. public void saveToFile(File file) throws

ID: 3846840 • Letter: I

Question

I want to write this to a binary file.

public void saveToFile(File file) throws IOException
{
BufferedWriter writer = new BufferedWriter(new FileWriter("studentInfo.txt"));
for(int i=0;i<students.size();i++)
{
String studentId= String.valueOf(students.get(i).getId());
String name= students.get(i).getFirstName()+" "+students.get(i).getLastName();
String birthday= students.get(i).getBirthday();
String address=students.get(i).getAddress();
writer.write(studentId);
writer.write(" ");
writer.write(name);
writer.write(" ");
writer.write(birthday);
writer.write(" ");
writer.write(address);
writer.write(" ");
}
writer.close();
}

Explanation / Answer

public void saveToFile(File file) throws IOException {
       // BufferedWriter writer = new BufferedWriter(new FileWriter(
       // "studentInfo.txt"));
       DataOutputStream writer = new DataOutputStream(new FileOutputStream(
               "studentInfo.txt"));
       for (int i = 0; i < students.size(); i++) {
           String studentId = String.valueOf(students.get(i).getId());
           String name = students.get(i).getFirstName() + " "
                   + students.get(i).getLastName();
           String birthday = students.get(i).getBirthday();
           String address = students.get(i).getAddress();
           writer.writeBytes(studentId);
           writer.writeBytes(" ");
           writer.writeBytes(name);
           writer.writeBytes(" ");
           writer.writeBytes(birthday);
           writer.writeBytes(" ");
           writer.writeBytes(address);
           writer.writeBytes(" ");
       }
       writer.close();
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote