Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Efficiency! Custom Serialization

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,558 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.

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,558 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.

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,558 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.

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

Thank You!!!