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 { // Implement this method PrintWriter outputLines = new PrintWriter ( outputStream ); if(users.isEmpty ()){ }if(!users.isEmpty ()){ for(User user : users){ //write first name if(user.getFirstName () == null){ outputLines.println ( "null" ); }else outputLines.println ( user.getFirstName () ); if(user.getLastName () == null){ outputLines.println ( "null" ); }else { outputLines.println ( user.getLastName () ); } //write birthday if(user.getBirthDate () == null){ outputLines.println ( "null" ); }else{ outputLines.println ( user.getBirthDate ()); } //write sex if(user.isMale ()){ outputLines.println ( "true" ); }else{ outputLines.println ( "false" ); } //write country if(user.getCountry () == null){ outputLines.println ( "null" ); }else{ outputLines.println ( user.getCountry ().getDisplayName () ); } outputLines.flush (); } } } public void load(InputStream inputStream) throws Exception { // Implement this method BufferedReader reader = new BufferedReader ( new InputStreamReader ( inputStream ) ); while(reader.ready ()) { User user = new User (); //write first name String firstName = reader.readLine (); if(firstName.equals ( "null" )){ user.setFirstName ( null ); }else { user.setFirstName (firstName); } //write last name String lastName = reader.readLine (); if(lastName.equals ( "null" )){ user.setLastName ( null ); }else { user.setLastName ( lastName ); } //write birthday String birthday = reader.readLine (); if(birthday.equals ( "null" )){ user.setBirthDate ( null ); }else{ user.setBirthDate (new SimpleDateFormat ( "dd-MMM-yyyy" ).parse ( birthday ) ); } //write sex String isMale = reader.readLine (); if(isMale.equals ( "true" )){ user.setMale ( true ); }else{ user.setMale ( false ); } //write country String country = reader.readLine (); if(country.equals ( "null" )){ user.setCountry ( null ); }else{ if(country.equals ( "United States" )){ user.setCountry ( User.Country.UNITED_STATES ); } if(country.equals ( "United Kingdom" )){ user.setCountry ( User.Country.UNITED_KINGDOM ); } if(country.equals ( "Other" )){ user.setCountry ( User.Country.OTHER ); } } users.add ( user ); } } @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; } } }