Больше не могу. Подскажите пожалуйста что не так.
Объекты сравниваются, всё нормально.
Но валидатор не проходит по первому условию.
Типа, в файл нужно сохранять объекты? Пробую сохранять через ObjectOutputStream, и файл появляется и пишется, но задача начинает ругаться "Oops, something is wrong with my file". Видимо уже совсем не туда лезу.
package com.javarush.task.task20.task2004;
import java.io.*;
import java.util.Properties;
/*
Читаем и пишем в файл статики
*/
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);
OutputStream outputStream = new FileOutputStream(yourFile);
InputStream inputStream = new FileInputStream(yourFile);
ClassWithStatic classWithStatic = new ClassWithStatic();
classWithStatic.i = 3;
classWithStatic.j = 4;
classWithStatic.save(outputStream);
outputStream.flush();
ClassWithStatic loadedObject = new ClassWithStatic();
loadedObject.staticString = "something";
loadedObject.i = 6;
loadedObject.j = 7;
loadedObject.load(inputStream);
//here check that the classWithStatic object is equal to the loadedObject object - проверьте тут, что classWithStatic и loadedObject равны
System.out.println(classWithStatic.equals(loadedObject) ? "The objects are equals" : "The objects are NOT equals");
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 ClassWithStatic {
public static String staticString = "This is a static test string";
public int i;
public int j;
public void save(OutputStream outputStream) throws Exception {
//implement this method - реализуйте этот метод
Properties properties = new Properties();
properties.put("i", this.i + "");
properties.put("j", this.j + "");
properties.store(outputStream, "");
// outputStream.write((this.i + "\n").getBytes());
// outputStream.write((this.j + "\n").getBytes());
}
public void load(InputStream inputStream) throws Exception {
//implement this method - реализуйте этот метод
Properties properties = new Properties();
properties.load(inputStream);
i = Integer.parseInt(properties.getProperty("i"));
j = Integer.parseInt(properties.getProperty("j"));
// String str = "";
// while (inputStream.available() > 0) {
// str += (char) inputStream.read();
// }
// String[] array = str.split("\\n");
// i = Integer.parseInt(array[0]);
// j = Integer.parseInt(array[1]);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassWithStatic that = (ClassWithStatic) o;
if (i != that.i) return false;
return j == that.j;
}
@Override
public int hashCode() {
int result = i;
result = 31 * result + j;
return result;
}
}
}