'Efficient object storage in a file in Java

I am writing a Spotify-like app for an assignment.I have a Song and a Playlist class and I have files where I would store the data. In the constructor of the Spotify Server, the files are supposed to be read and their data transformed into Song and Playlist objects, which the client would then be able to play. For example, when the Server is run for the first time, the files are empty and there are no songs and playlists. An administrator/user adds several songs and playlists. They are stored in those files. The application is terminated. When the Server is run again, the songs and playlists that have been added in all previous sessions (and stored in files) are loaded and a user can play them. The question is what technique should I use to store the object data in the most efficient way. I have considered the following options:

  1. A database - I haven't studied how to make one and that's not the idea of the assignment.
  2. JSON - I know that JSON is an effective way to store and send information in the network. But here, I won't be sending the information across the network. The server will read it from its local hardware. If I use JSON, I will make use of the GSON library.
  3. Serialization - I could do it the traditional way. Make Song and Playlist implement Serializable and store the information in the files.
    The structure of the classes is as follows:
public class Song{
   private final UUID id;
   private final String artist;
   private final String name;
   private final int duration;
   private int timesListenedTo = 0;
   private final Path filePath; // a non-changing path to the .wav audio file of the song
}
public class Playlist{
   private List<Song> songs; // I could replace this with a List<String> or List<UUID> if considered more efficient
   private final String name;
}


What is the most resource-efficient approach for my task? My priorities in order are:

  1. Minimal CPU usage
  2. Minimal memory usage
  3. Code complexity (the least important)


Solution 1:[1]

You can Perst give a try.

Perst is McObject’s open source, dual license, object-oriented embedded database system (ODBMS). It is available in one edition developed as an all-Java embedded database, and another implemented in C# (for Microsoft .NET Framework applications). Perst gives developers the ability to store, sort and retrieve objects in their applications with maximum speed and with low memory and storage overhead, while leveraging the object-oriented paradigm of Java and C#.

Solution 2:[2]

you can convert the data object to bytes array and write it to a file,but if you think the java native api have a little slow ,you can try protobuf,wish you can get your way

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan D.
Solution 2 zichuan