I am working on a learning project, that is supposed to import and export Flashcards to a txt.file which is later supposed to be used in questioning.
Currently, I am stuck on the import part and the research I did is not really working, because I am not really getting it.
I have this overall map where I save the term: definition in a map
private var flashCardMap = mutableMapOf<String, String>()
Then I have this export function
private fun export() {
println("File name:")
scan.nextLine()
val fileName = scan.nextLine()
val myFile = File(fileName)
try {
myFile.bufferedWriter().use { out->
flashCardMap.forEach {
out.write("${it.key}:${it.value}\n")
} }
println("${flashCardMap.size} cards have been saved.")
} catch (e: FileNotFoundException) {
println("File not Found.")
}
}
which exports all the cards I defined earlier in a txt. file like this with one or more Flashcard (Card = Definition)
key:value
AND here is where I am stuck. I try to import a .txt file and the including map but it does not work. It Is supposed to import the map and tell me how many Cards where imported and the add them to my current flashcardmap with which I am working in this session. Here is what I tried:
private fun import() {
println("File name:")
scan.nextLine()
val fileName = scan.nextLine()
val myFile = File("$fileName")
try {
val importMap =
myFile.readLines().chunked(2) {
it[0] to it[1]
}.toMap()
println("${importMap.size} cards have been loaded.")
flashCardMap.putAll(importMap)
} catch (e: FileNotFoundException) {
println("File not Found.")
}
}