Java Java Data Structures Efficiency! Custom Serialization

Jonathan Leon
Jonathan Leon
18,813 Points

When loading the txt file I get all the songs but when I quit and start the again it keeps only #1 song

import com.teamtreehouse.KaraokeMachine;
import com.teamtreehouse.model.SongBook;
import com.teamtreehouse.model.SongBook;
import com.teamtreehouse.model.SongBook;

public class Karaoke {

  public static void main(String[] args) {
    SongBook songBook = new SongBook();
    KaraokeMachine machine = new KaraokeMachine(songBook);;
    System.out.println("Saving book....");
package com.teamtreehouse.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Comparator;
import java.util.Map;

public class SongBook {
  private List<Song> mSongs;

  public SongBook() {
    mSongs = new ArrayList<Song>();

  public void exportTo(String fileName) {

    try (
            FileOutputStream fos = new FileOutputStream(fileName);
            PrintWriter writer = new PrintWriter(fos);

        ) {
              for (Song song: mSongs) {



          } catch(IOException ioe) {

            System.out.printf("Problem with saving %s %n", fileName);

    public void importFrom(String fileName) {

          try (

                FileInputStream fis = new FileInputStream(fileName);
                BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
          ) {

            String line;
            while ((line = reader.readLine()) != null ) {

              String[] args = line.split("\\|");

              addSong(new Song(args[0],args[1],args[2]));


          } catch (IOException ioe) {

            System.out.printf("There was a problem loading %s %n", fileName);



  public void addSong(Song song) {

  public int getSongCount() {
    return mSongs.size();
  //FIX ME this should be cached
  private Map<String,List<Song>> byArtist() {

    Map<String,List<Song>> byArtist = new TreeMap<>();

    for (Song song : mSongs) {

     List<Song> artistSongs = byArtist.get(song.getArtist());

      if (artistSongs == null) {

        artistSongs = new ArrayList<>();


        return byArtist;


    public Set<String> getArtists() {

      return byArtist().keySet();


    public List<Song> getSongsForArtist(String artistName) {

      List<Song> songs = byArtist().get(artistName);

      songs.sort(new Comparator<Song>() {
      public int compare(Song song1, Song song2) {

        if (song1 == song2) {
          return 0;
          return song1.mTitle.compareTo(song2.mTitle);


        return songs;        

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 59,156 Points

Hi Jonathan, It is quite an old question. The key is to look at the songs.txt file before and after running. The new version created by your code had all songs on one line. Thus when read back in, only the first song would make it and the rest of the line would be discarded.

Looking at your exportTo method, it is missing a training NEWLINE. Fixed here:

 writer.printf("%s|%s|%s%n",  // added %n for newline

Difference of songs.txt before and after execution:

$ diff songs.txt*
< Michael Jackson|Beat It| 5|I want you back| 5|ABC||Don't Stop Believin'| Phillips|Hold On| Swift|Shake It Off| Perry|Roar| Valens|La Bamba||With Or Without You||I Still Haven't Found| 5|Rockin' Robin| Cure|Friday I'm in Love| Cure|Boys Don't Cry| Cure|Just Like Heaven| Cure|Love Song|
\ No newline at end of file
> Michael Jackson|Beat It|
> Jackson 5|I want you back|
> Jackson 5|ABC|
> Journey|Don't Stop Believin'|
> Wilson Phillips|Hold On|
> Taylor Swift|Shake It Off|
> Katy Perry|Roar|
> Ritchie Valens|La Bamba|
> U2|With Or Without You|
> U2|I Still Haven't Found|
> Jackson 5|Rockin' Robin|
> The Cure|Friday I'm in Love|
> The Cure|Boys Don't Cry|
> The Cure|Just Like Heaven|
> The Cure|Love Song|