So this "clue" from the mentor does absolutely no good at helping me out. I haven't a clue what to do and I've been reading like crazy to try to figure this out and I all I keep reading everywhere is that it is 1000x easier and safer and better to just use serialization.... please help. Thanks in advance : )
package com.codegym.task.task20.task2002;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
Reading and writing to a file: CodeGym
*/
public class Solution {
public static void main(String[] args) {
// You can find your_file_name.tmp in your TMP directory or adjust outputStream/inputStream according to your file's actual location
try {
File yourFile = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
CodeGym codeGym = new CodeGym();
// Initialize users field for the codeGym object here
codeGym.save(outputStream);
outputStream.flush();
CodeGym loadedObject = new CodeGym();
loadedObject.load(inputStream);
// Here check that the codeGym object is equal to the loadedObject object
outputStream.close();
inputStream.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with my file");
} catch (Exception e) {
//e.printStackTrace();
System.out.println("Oops, something is wrong with the save/load method");
}
}
public static class CodeGym {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
PrintStream out = new PrintStream(outputStream);
if (users.size()==0) {
out.print("null");
}
else {
for (User u : users) {
String firstName = u.getFirstName();
String lastName = u.getLastName();
String birthday = u.getBirthDate().toString();
String sex = u.isMale()? "true":"false";
String country = u.getCountry().toString();
out.print(firstName +","+ lastName +","+ birthday +","+ sex +","+ country);
}
}
}
public void load(InputStream inputStream) throws Exception {
BufferedReader bR = new BufferedReader(new InputStreamReader(inputStream));
String in = bR.readLine();
if (!in.equals("null")) {
String[] inStrings = in.split(",");
String firstName = inStrings[0];
String lastName = inStrings[1];
String birthday = inStrings[2];
String sex = inStrings[3];
String country = inStrings[4];
User user = new User();
//Name
user.setFirstName(firstName);
user.setLastName(lastName);
//Birthday
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss");
Date parsedDate = formatter.parse(birthday);
user.setBirthDate(parsedDate);
//Sex
if (sex.equals("true")) {
user.setMale(true);
}
else {
user.setMale(false);
}
//country
if (country.equals("United States")) {
user.setCountry(User.Country.UNITED_STATES);
}
if (country.equals("United Kingdom")) {
user.setCountry(User.Country.UNITED_KINGDOM);
}
else {
user.setCountry(User.Country.OTHER);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CodeGym codeGym = (CodeGym) o;
return users != null ? users.equals(codeGym.users) : codeGym.users == null;
}
@Override
public int hashCode() {
return users != null ? users.hashCode() : 0;
}
}
}