This commit is contained in:
2024-05-01 13:24:03 +02:00
parent b7c2adae27
commit 6d1d53e43c
5 changed files with 45 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
package ch.hevs.isc.slopify_v2
import java.io.File
import java.io.{File, FileInputStream, FileOutputStream, ObjectInput, ObjectInputStream, ObjectOutputStream}
object DataBaseHelper {
def create(directory:String) : DataBase = {
@@ -52,11 +52,30 @@ object DataBaseHelper {
return db
}
def save(fileName:String, db:DataBase) : Unit = ???
def load(fileName:String) : DataBase = ???
def save(fileName: String, db: DataBase) : Unit = {
val fos: FileOutputStream = new FileOutputStream(fileName)
val oos: ObjectOutputStream = new ObjectOutputStream(fos)
oos.writeObject(db)
oos.close()
}
def load(fileName:String) : DataBase = {
val fis: FileInputStream = new FileInputStream(fileName)
val ois: ObjectInputStream = new ObjectInputStream(fis)
val db: DataBase = ois.readObject().asInstanceOf[DataBase]
ois.close()
return db
}
def main(args: Array[String]): Unit = {
val db: DataBase = create("res/songs")
println(db)
val artists: Array[Artist] = db.getArtists()
println(artists(0))
save("res/db.bin", db)
val db2: DataBase = load("res/db.bin")
println(db2)
val artists2: Array[Artist] = db2.getArtists()
println(artists2(0))
}
}