I want to write objects in a file by using arraylist, what should i add to display my file?
package my.issues.issue15;
import java.io.*;
import java.util.ArrayList;
public class WriteObjects {
public static void main(String[] args) throws IOException {
Result r1 = new Result("101","science","3","A");
Result r2 = new Result("102"," Nationhood Studies","3","B+");
Result r3 = new Result("103","Math","3","A-");
ArrayList<Result> resultList = new ArrayList<>();
resultList.add(r1);
resultList.add(r2);
resultList.add(r3);
System.out.println(resultList);
FileOutputStream fos = new FileOutputStream("Course.ser");
ObjectOutputStream oos =new ObjectOutputStream(fos);
oos.writeObject(resultList);
System.out.println("Finish");
oos.close();
fos.close();
}
}
It shows error if i use the buffer writer. What should i add so that my file will be able to display?
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("Results.txt"), true));
writer.write(resultList);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
Error
Error:(21, 19) java: no suitable method found for write(java.util.ArrayList<my.issues.issue15.Result>)
method java.io.Writer.write(char[]) is not applicable
(argument mismatch; java.util.ArrayList<my.issues.issue15.Result> cannot be converted to char[])
method java.io.Writer.write(java.lang.String) is not applicable
(argument mismatch; java.util.ArrayList<my.issues.issue15.Result> cannot be converted to java.lang.String)
method java.io.BufferedWriter.write(int) is not applicable
(argument mismatch; java.util.ArrayList<my.issues.issue15.Result> cannot be converted to int)
Below are the details of my Result class. i would like to figure out what should i add so that the file will be created
import java.io.Serializable;
public class Result implements Serializable {
private String courseCode;
private String courseName;
private String creditHours;
private String grade;
public Result(String courseCode, String courseName, String creditHours, String grade) {
this.courseCode = courseCode;
this.courseName = courseName;
this.creditHours = creditHours;
this.grade = grade;
}
public String toString(){
return "< " + courseCode + " | " + courseName + " | " + "Credit Hours: " + creditHours + " | " + "Grade: " +grade + " >";
}
}