From 532cd1e395a637a5e507bc3687a4a64e50e55b30 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 11 Dec 2020 09:12:05 +0100 Subject: adding Serialization interface and methods --- Song.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'Song.java') diff --git a/Song.java b/Song.java index c4d1fc3..ecbb1eb 100644 --- a/Song.java +++ b/Song.java @@ -1,4 +1,4 @@ -public class Song extends MusicalElement { +public class Song extends MusicalElement{ private String artist, genre; public Song(String title, int duration, String content, String artist, Genre genre) { super(title, duration, content); @@ -15,5 +15,28 @@ public class Song extends MusicalElement { return getTitle()+";"+getDuration()+";"+getContent()+";"+artist+";"+genre; } + public void save(){ + try + { + //Saving of object in a file + FileOutputStream file = new FileOutputStream(songs.csv); + ObjectOutputStream out = new ObjectOutputStream(file); + + // Method for serialization of object + out.writeObject(this); + + out.close(); + file.close(); + + System.out.println("Object has been serialized"); + + } + + catch(IOException ex) + { + System.out.println("IOException is caught"); + } + } + } -- cgit v1.2.3