1. I was able to pass this task, but I have a question about the "Write the verification code yourself in the main method:". I followed the steps and came up with the code bellow:
Steps:
1) create a file, open an input stream and an output stream;
2) create an instance of the Solution class (savedObject);
3) write savedObject to the output stream (make sure that they're really there);
4) create another instance of the Solution class with a different argument;
5) load an object from the input stream (loadedObject);
6) verify that savedObject.string is equal to loadedObject.string;
7) handle exceptions.
Code:
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Username\\Desktop\\test.txt"); //I changed my username to post here
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Username\\Desktop\\test.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
Solution savedObject = new Solution(1);
objectOutputStream.writeObject(savedObject);
objectInputStream.close();
Solution loadedObject = (Solution) objectInputStream.readObject();
objectInputStream.close();
System.out.println(savedObject.equals(loadedObject));
System.out.println(new Solution(4));
}
When I run this code, I get the exception:
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at codegym.Solution.main(Solution.java:18)
What have I done wrong? What should be the correct output for the required steps?
When it comes to real files in my computer, I'm never able to get it done haha. It never works when I try to do the CRUD things here.
--
2. How do I know which fields I don't need to serialize?
I read the requirements and passed the task, but I didn't really understand why pattern, currentDate and temperature must be marked with transient (i.e. not to be serialized) and string must not. I've read some articles telling what the transient marker does. I just don't understand what's the criteria of the application here.
Thanks!