CodeGym /Courses /Java Core /Saving an Object array to a file

Saving an Object array to a file

Java Core
Level 10 , Lesson 2
Available

"Hello, Amigo!"

20
Task
Java Core, level 10, lesson 2
Locked
Reading and writing to a file: Human
Implement the logic for file IO for the Human class. The Human class's name field can't be empty. The main method exists just for you and is not involved in testing the solution.
20
Task
Java Core, level 10, lesson 2
Locked
Reading and writing to a file: CodeGym
Implement the logic for file IO for the CodeGym class. The file your_file_name.tmp can contain several CodeGym objects. The main method exists just for you and is not involved in testing the solution.
20
Task
Java Core, level 10, lesson 2
Locked
Introducing properties
In the fillInPropertiesMap method, read a file name from the console and populate the properties map with data from the file. You can read about properties here: http://en.wikipedia.org/wiki/.properties Implement the logic for file IO for the properties map.
12
Task
Java Core, level 10, lesson 2
Locked
Reading and writing static fields to a file
Implement the logic for file IO for the ClassWithStatic class. The load method must initialize the object, including the static fields with data from the file. The main method is not tested.
12
Task
Java Core, level 10, lesson 2
Locked
Stranger things
Strange errors are occurring while reading/writing Human objects. Figure out what's wrong and fix it.
Comments (30)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Evgeniia Shabaeva Level 34, Budapest, Hungary
6 November 2024
Wow, this has been tough!
Gellert Varga Level 23, Szekesfehervar, Hungary
22 November 2021
There are companies where the use of the ternary operator in code is explicitly forbidden. I read it in an article written by a computer science teacher, who used to work as a developer for several software companies. Ternary is hard to understand, it makes difficult to review the code. And it's a very important aspect.
Nyerges Ferenc Level 31, Bükkábrány, Hungary
28 November 2020
A hint for the ones about to start wokring on these tasks: have a look at Java Properties class
Andrei Level 41
24 February 2021
What am I supposed to do with it, lol ! ? I looked at it after the first exercise, I literally had no clue what its use could be. 😂
Andrei Level 41
25 February 2021
OK, I understand it a bit better now . 😀
AlfredW Level 23, Denver, United States
5 November 2020
These were exceedingly hard to do. I would suggest that before anyone even tries them that you read all of the various comments and questions and answers for each task. I think a major part of why these are so hard is that they are not explained well. I find it all but impossible to figure out what it is they are looking for. The Conditions are so unclear, even to the point of being obtuse.
Angus McDonald Level 5, Sydney, Australia Expert
10 June 2020
This has got exceedingly more difficult in a very short space of lessons. Enjoying it but have nearly thrown my mouse across the room on a couple occasions lol.
Angus McDonald Level 5, Sydney, Australia Expert
10 June 2020
For example.. the if(ternary operator) has made my brain feel like sludge. So many null/not nulls (if not null/null)..Nullll = maybe false?.. or is it true?... urrghh.
Daniel Walbolt Level 22, Waterville, United States
9 August 2020
It's basically a nested if statement used for short-hand notation. Although it is not a fun time for code readers because it's so complex for being that compact. Comments are needed for sure. I explained it more thoroughly below \/
Agent Smith Level 38
14 September 2020
IntelliJ suggested to replace a few nested ternary operators with this:

Objects.equals(o1, o2.param);
Looks nice and simple to read. :)
Fadi Alsaidi Level 34, Carrollton, TX, USA
29 May 2020
does anyone find this code a little odd? as if it was done backward!!! if (name != null ? !name.equals(human.name) : human.name != null) return false;
Daniel Walbolt Level 22, Waterville, United States
9 August 2020
Actually, this code is just a very-short hand way of making sure the objects are the same even IF they are null. The name of the Human object can be null or have a value. This line of code checks if the name if NOT null. If it is not null, the if statement reads the OPPOSITE (!) of whether or not the name of human1 equals human2's name. If the names are not the same (equals is FALSE) then the if statement is ACTIVATED and "return false;' is executed. In the other scenario, when name IS null, it checks if the human2's name variable is NOT null. If the second name is NOT null then the names do not match because one is null and the other is not.
Attila Level 25
15 May 2020
Help for : Stranger things 1. Look at the equals() and the hashCode() methods. 2. Try to understand what they do. 3. If you dont understand (like me) than look at the same methods in the previous 4 Tasks. Basically it should be almost the same code. 4. (Optional) If you found the errors, try to understand the code again.
MaGaby2280 Level 41, Guatemala City, Guatemala
24 April 2020
Hard exercises, but most of all, some don´t make sense to me ;o(
Johannes Level 27, Centurion, Pretoria, South-Africa
22 April 2020
Tip for the CodeGym task: The requirements are VERY HAZY. (1) In the SAVE, they want you to firstly check if there's anything in the users LIST. Only save if : if (!users.isEmpty()) { all your save code } (2) Before saving it, save the first line as the size of the users LIST outputStream.write(String.valueOf(users.size()).getBytes()); Also read this, to know how many times to load it, in the LOAD method (3)Then, for each field, save to lines. Line 1 = whether the field is null or not, Line 2 is the field value itself. (4) Remember to flush() when exiting your SAVE loop (for some stupid reason. Don't understand why the write in ANY language would need a flush afterwards. It's not supposed to be a toilet. When you READ/LOAD, do the same: Each statement that reads a value, should first read the line that says if the value is null or not (that you saved). Then after that, read the line after, which has the value, and set your user object's value by calling the corresponding method in User class. Remember to close the reader. This was not a very clear or nice task, to say the very least. I really enjoyed the course up to this level. This level can be improved a lot, but let's move on!
Manish Sinha Level 26, london, United Kingdom
10 May 2020
• writer.flush(): This function forces all the data stored internally in the writer buffer to be "released" to the target source (String, console output, File). I once was printing to the console a text from writer and the result was an empty string. Very strange. Then I called the flush() method and voila, finally had all my data printed. (copied answer from previous lessone).Hope this helps.
Johannes Level 27, Centurion, Pretoria, South-Africa
12 May 2020
hi Manish, thanks, but it changes nothing to my statement: it's stupid that any coding language would need a flush() method to print if the primary print() just refuses to print ?
- FniJnn - Level 33, Paris, France
13 December 2019
The 2002 task took me so much time. Ill give you a hint. If you want to convert a date.toString() to date, use this. Date date = new Date(); String s = date.toString(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT); Date birthDay = Date.from(ZonedDateTime.parse(s, dtf).toInstant()); Good luck ;)
Csaba Gódor Level 36, Budapest, Hungary
15 January 2020
what's wrong with the simple

                    printWriter.println(user.getBirthDate().getTime());
...
 user.setBirthDate(new Date(Long.parseLong(bufferedReader.readLine())));
one ?
- FniJnn - Level 33, Paris, France
15 January 2020
getTime() returns a long not a formated string
Justin Johnson Level 31, Clearwater, United States
29 February 2020
all I did was... writer.println(u.getBirthDate()); and user.setBirthDate(new Date(reader.readLine()));
JianQiu Hwang Level 35, Washington
14 March 2020
Nothing wrong, and I have passed the test by the simple one.