简体   繁体   中英

How to write objects in a file using arraylist

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 + " >";
    }
}

You are trying to call write(...) which expects either a char array, a String or an Integer as parameter (The error message literally tells you " ArrayList<...> cannot be converted to char[]/String/int ).

Since I don't know how your Result class looks like, I can't write the code for you, but basically you need to get the Values from each Result object and either write them to File one by one or use a StringBuilder to build the full String that you want to write.

It would probably look something like this:

StringBuilder sb = new StringBuilder();

for(Result result : resultList)
{
    String[] resultData = result.getValues(); // getValues() is whatever getters you use in result class
    
    for(String substring : resultData)
    {
        sb.append(substring)
    }
}

And then you can call toString() on the StringBuilder instane and write that to your file.

Nevermind it's way simpler than that, all you need to do is call toString() on the ArrayList.

Full class:

import java.io.*;
import java.util.ArrayList;

public class WriteObjects
{
    public static void main(String[] args)
    {
        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);
        
        try
        {
            File file = new File("Results.txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
            writer.write(resultList.toString());
            writer.newLine();
            writer.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM