Что то я не пойму что с Датой до миллисекунд все в тесте работает вот вывод в консоль
// save
30 05 2008 07:20 12 121
06 02 2040 08:06 52 122
//load
30 05 2008 07:20 12 121
06 02 2040 08:06 52 122
Рекомендация Ментора
Убедись, что дни рождения пользователей корректно записываются и читаются, с точностью до миллисекунд.package com.javarush.task.task20.task2002;
import java.io.*;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
/*
Читаем и пишем в файл: JavaRush
*/
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
//вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
try {
File yourFile = File.createTempFile("your_file_name", null, new File("E:/"));
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
JavaRush javaRush = new JavaRush();
//initialize users field for the javaRush object here - инициализируйте поле users для объекта javaRush тут
User ivan = new User();
ivan.setFirstName("Ivan");
ivan.setLastName("Crow");
ivan.setBirthDate(new Date(1212121212121L));
ivan.setMale(true);
ivan.setCountry(User.Country.UKRAINE);
javaRush.users.add(ivan);
javaRush.save(outputStream);
outputStream.flush();
JavaRush javaRush1 = new JavaRush();
javaRush1.save(outputStream);
outputStream.flush();
User ivan3 = new User();
ivan3.setFirstName("Ivan3");
ivan3.setLastName("Crow3");
ivan3.setBirthDate(new Date(2212121212122L));
ivan3.setMale(true);
ivan3.setCountry(User.Country.UKRAINE);
javaRush.users.add(ivan3);
javaRush.save(outputStream);
outputStream.flush();
JavaRush loadedObject = new JavaRush();
loadedObject.load(inputStream);
//here check that the javaRush object is equal to the loadedObject object - проверьте тут, что javaRush и loadedObject равны
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 JavaRush {
public List<User> users = new ArrayList<>();
public void save(OutputStream outputStream) throws Exception {
//implement this method - реализуйте этот метод
String stLine = "";
if (users.isEmpty()){
stLine = "isEmpty\n";
outputStream.write(stLine.getBytes());
} else {
for (User cell : users){
stLine = cell.getFirstName() + ", " + cell.getLastName() + ", " + new SimpleDateFormat("dd MM yyyy hh:mm s S").format(cell.getBirthDate()) +
", " + cell.isMale() + ", "+ cell.getCountry() + "\n";
outputStream.write(stLine.getBytes());
}
users.remove(0);
}
stLine = null;
}
public void load(InputStream inputStream) throws Exception {
//implement this method - реализуйте этот метод
StringBuilder stFileBuild = new StringBuilder();
while (inputStream.available()>0){
stFileBuild.append((char) inputStream.read());
}
String[] stLine = stFileBuild.toString().split("\n");
User person = null;
for (int i = 0; i < stLine.length; i++){
if (stLine[i].equals("isEmpty")){
JavaRush javaRush = new JavaRush();
} else {
person = new User();
String[] oneLine = stLine[i].split(", ");
person.setFirstName(oneLine[0]);
person.setLastName(oneLine[1]);
String format = "dd MM yyyy hh:mm s S";
Date dateBirthday = new SimpleDateFormat(format).parse(oneLine[2]);
person.setBirthDate(dateBirthday);
if (oneLine[3].equals("true")){
person.setMale(true);
} else if (oneLine[3].equals("false")){
person.setMale(false);
}
for (int b = 0; b < User.Country.values().length; b++){
String stEnum = String.valueOf(User.Country.values()[b]);
if (oneLine[4].toUpperCase().equals(stEnum)){
person.setCountry(User.Country.values()[b]);
}
stEnum = null;
}
users.add(person);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JavaRush javaRush = (JavaRush) o;
return users != null ? users.equals(javaRush.users) : javaRush.users == null;
}
@Override
public int hashCode() {
return users != null ? users.hashCode() : 0;
}
}
}