#File encoding error after serialization

1 messages · Page 1 of 1 (latest)

minor berry
#

The main idea of the program is that it retrieves a list of objects from a file and add a new element to that list

carmine nacelleBOT
#

<@&987246399047479336> please have a look, thanks.

carmine nacelleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

minor berry
#
public class UserService {
    private UserView userView;
    private List<Student> studentList = new ArrayList<>();

    public UserService(UserView userView) {
        this.userView = userView;
    }

    public void addNewStudentToList() {
        Student student = userView.readStudentInformations();
        retrieveListOfStudents();
        studentList.add(student);
        addToList();
    }

    public void addToList() {
        try {
            FileOutputStream fout= new FileOutputStream ("test.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(studentList);
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void retrieveListOfStudents() {
        try {
            FileInputStream fin= new FileInputStream ("test.ser");
            ObjectInputStream ois = new ObjectInputStream(fin);
            studentList = (ArrayList<Student>)ois.readObject();
            fin.close();

            studentList.forEach(System.out::println);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}  
carmine nacelleBOT
iron rampart
#

@minor berry what's the problem?

minor berry
#

how can I modify this to a normal text

minor berry
iron rampart
minor berry
#

I know

iron rampart
#

What you are asking is like "how to convert png to normal text"

minor berry
#

yes

#

even if I switch to another encoding format results the same

iron rampart
#

Yes that's normal

#

What do you think opening a png as text and trying to change encoding will do

#

I don't understand what you are trying to do here

#

@minor berry

minor berry
#

I'm trying to add a new element to a list that I retrieve from that test2.ser file

iron rampart
#

Then add it

#

list.add

minor berry
#

I did it, it works

#

but I was hoping to get a human-readable file in test.ser

iron rampart
#

Well no

minor berry
#

for ex to see each student on a new row

iron rampart
#

That's not the goal of java default serialization mechanism

minor berry
#

oh

#

is there a way to achieve what I want?

iron rampart
#

If you want human readable format, use a lib like Jackson and serialize as json

minor berry
#

aha got it

#

thanks for your help, I will try this way