task 2
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user