#File encoding error after serialization
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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();
}
}
}
Detected code, here are some useful tools:
@minor berry what's the problem?
this is the output I get
This is not supposed to be normal text
I know
What you are asking is like "how to convert png to normal text"
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
I'm trying to add a new element to a list that I retrieve from that test2.ser file
Well no
for ex to see each student on a new row
That's not the goal of java default serialization mechanism
If you want human readable format, use a lib like Jackson and serialize as json