Не могу понять, почему не проходит валидацию. Правильное решение смотрел, пишется построчно, считывается построчно, в моем решение при записи все конкатинируется в строку, пишется в строку, при загрузке строка парсится и нужные параметры заносятся, сравнение объектов так же выдает true, как и при правильном решении. Может быть перемудрил, но под конкретные данные условия задачи подходит же) необходимые изменения кода прикладываю
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 {
PrintWriter printWriter = new PrintWriter(outputStream);
String line = staticString + " " +this.i + " " + this.j;
printWriter.println(line);
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null){
Pattern pattern = Pattern.compile("(.+) (\\d+) (\\d+)");
Matcher matcher = pattern.matcher(line);
if (matcher.find()){
staticString = matcher.group(1);
this.i = Integer.parseInt(matcher.group(2));
this.j = Integer.parseInt(matcher.group(3));
} else throw new IllegalStateException();
}
reader.close();
}
package com.javarush.task.task20.task2004;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Читаем и пишем в файл статики
*/
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);
System.out.println(classWithStatic.equals(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 ClassWithStatic {
public static String staticString = "This is a static test string";
public int i;
public int j;
public void save(OutputStream outputStream) throws Exception {
PrintWriter printWriter = new PrintWriter(outputStream);
String line = staticString + " " +this.i + " " + this.j;
printWriter.println(line);
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null){
Pattern pattern = Pattern.compile("(.+) (\\d+) (\\d+)");
Matcher matcher = pattern.matcher(line);
if (matcher.find()){
staticString = matcher.group(1);
this.i = Integer.parseInt(matcher.group(2));
this.j = Integer.parseInt(matcher.group(3));
} else throw new IllegalStateException();
}
reader.close();
}
@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;
}
}
}