Java Java Data Structures Efficiency! Custom Serialization

Araz kuberin
Araz kuberin
6,045 Points

There is no "songs.txt" file how can the program run without throwing something like FileNotfoundException???

When craig is adding a new song it will not create a file it just adds that song to a List!

so how come we do not get FileNotFoundException or something ??

4 Answers

andren
andren
28,384 Points

The file was not created when the song was added, it was created when he quit the program. The last line of code in the main function is this:

songBook.exportTo("songs.txt");

The exportTo function was defined earlier and saves the song list to the disk.

Araz kuberin
Araz kuberin
6,045 Points

OK.. but first it imports when there is no song.txt file it should first export then importffrom. like..

public class Karaoke {

public static void main(String[] args) {

SongBook songBook = new SongBook();
songBook.exportTo("songs.txt");  // this will create a file songs.txt
System.out.println("Saving book...."); 
songBook.importFrom("songs.txt"); // here we can read it when the file songs.txt has been created 
KaraokeMachine machine = new KaraokeMachine(songBook);
machine.run();  

} }

andren
andren
28,384 Points

Yes, and the first time the application is launched an error does actually occur as you can see at 9:18 in the video. But there is a catch clause setup for the exception which is why the program does not crash. I assumed you were asking about why the error did not occur the second time the application was launched.

Araz kuberin
Araz kuberin
6,045 Points

OK I understand now...but i just wonder if the program catches first time when trying to importFrom the catch should print System.out.printf("Problems loading %s %n", fileName); why does that not be printed in the video as well ?

andren
andren
28,384 Points

It was printed above the stack trace, the stack trace is just so long that it pushed the printed string above what you ended up seeing of the console in the video. If Craig had scrolled up when the error occurred you would have seen the error message string at the top.

Araz kuberin
Araz kuberin
6,045 Points

And i think in the second time we have already created a file therefore the error doesnt accour!