//Jesus answered, "My Kingdom is not of this world. If my Kingdom were of this world, then my servants would fight, that I wouldn't be delivered to the Jews. But now my Kingdom is not from here." (John 18:36) package com.javarush.task.task20.task2004; import java.io.*; import java.util.Properties; /* ЧиÑаем и пиÑем в Ñайл ÑÑаÑики */ public class Solution { public static void main(String[] args) { try { File your_file_name = new File("E:\\5.txt"); OutputStream outputStream = new FileOutputStream(your_file_name); InputStream inputStream = new FileInputStream(your_file_name); 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); outputStream.close(); inputStream.close(); System.out.println(ClassWithStatic.staticString); System.out.println(loadedObject.i); System.out.println(loadedObject.j); } catch (IOException e) { System.out.println("Oops, something wrong with my file"); } catch (Exception e) { e.printStackTrace(); System.out.println("Oops, something wrong with save/load method"); } } public static class ClassWithStatic { public static String staticString = "it's test static string"; public int i; public int j; public void save(OutputStream outputStream) throws Exception { PrintWriter printWriter = new PrintWriter(outputStream); printWriter.println(staticString); printWriter.println(i); printWriter.println(j); printWriter.close(); } public void load(InputStream inputStream) throws Exception { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); ClassWithStatic.staticString = bufferedReader.readLine(); this.i = Integer.parseInt(bufferedReader.readLine()); this.j = Integer.parseInt(bufferedReader.readLine()); bufferedReader.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; } } /* ЧиÑаем и пиÑем в Ñайл ÑÑаÑики РеализÑй Ð»Ð¾Ð³Ð¸ÐºÑ Ð·Ð°Ð¿Ð¸Ñи в Ñайл и ÑÑÐµÐ½Ð¸Ñ Ð¸Ð· Ñайла Ð´Ð»Ñ ÐºÐ»Ð°ÑÑа ClassWithStatic. ÐеÑод load должен иниÑиализиÑоваÑÑ Ð¾Ð±ÑÐµÐºÑ Ð²ÐºÐ»ÑÑÐ°Ñ ÑÑаÑиÑеÑкие Ð¿Ð¾Ð»Ñ Ð´Ð°Ð½Ð½Ñми из Ñайла. ÐеÑод main не ÑÑаÑÑвÑÐµÑ Ð² ÑеÑÑиÑовании. ТÑебованиÑ: 1.âÐолжна бÑÑÑ Ñеализована возможноÑÑÑ ÑÐ¾Ñ ÑанениÑ/загÑÑзки обÑекÑов Ñипа Solution.ClassWithStatic Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Ð¼ÐµÑодов save/load. 2.âÐлаÑÑ Solution не должен поддеÑживаÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Serializable. 3.âÐлаÑÑ Solution.ClassWithStatic не должен поддеÑживаÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Serializable. 4.âÐлаÑÑ Solution.ClassWithStatic должен бÑÑÑ Ð¿ÑблиÑнÑм. 5.âÐлаÑÑ Solution.ClassWithStatic не должен поддеÑживаÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ Externalizable. package com.javarush.task.task20.task2004; import java.io.*; * ЧиÑаем и пиÑем в Ñайл ÑÑаÑики * public class Solution { public static void main(String[] args) { //you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream according to your real file location //Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе найÑи your_file_name.tmp в папке TMP или иÑпÑавÑÑе outputStream/inputStream в ÑооÑвеÑÑÑвии Ñ Ð¿ÑÑем к ваÑÐµÐ¼Ñ ÑеалÑÐ½Ð¾Ð¼Ñ ÑÐ°Ð¹Ð»Ñ try { File your_file_name = File.createTempFile("your_file_name", null); OutputStream outputStream = new FileOutputStream(your_file_name); InputStream inputStream = new FileInputStream(your_file_name); 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); //check here that classWithStatic object equals to loadedObject object - пÑовеÑÑÑе ÑÑÑ, ÑÑо classWithStatic и loadedObject ÑÐ°Ð²Ð½Ñ outputStream.close(); inputStream.close(); } catch (IOException e) { //e.printStackTrace(); System.out.println("Oops, something wrong with my file"); } catch (Exception e) { //e.printStackTrace(); System.out.println("Oops, something wrong with save/load method"); } } public static class ClassWithStatic { public static String staticString = "it's test static string"; public int i; public int j; public void save(OutputStream outputStream) throws Exception { //implement this method - ÑеализÑйÑе ÑÑÐ¾Ñ Ð¼ÐµÑод } public void load(InputStream inputStream) throws Exception { //implement this method - ÑеализÑйÑе ÑÑÐ¾Ñ Ð¼ÐµÑод } @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; } } } */