diff --git a/console/src/main/kotlin/notes/multi/console/Console.kt b/console/src/main/kotlin/notes/multi/console/Console.kt index 3eeb7d03a58326aedcee99d49db190c816e30c51..de4eb37ec3e2c76a0f41fc7488348a985394d0d2 100644 --- a/console/src/main/kotlin/notes/multi/console/Console.kt +++ b/console/src/main/kotlin/notes/multi/console/Console.kt @@ -38,13 +38,13 @@ fun main(args: Array<String>) { } else { if (args.size < 2) { val filename = args[0] - // Optional Regex Check for a specific argument format: - MessageUtils.verifyFilename(filename, Regex("^.*[.](md|txt)$")) - // if (fileExists) { - // Filemanager(".").editfile() - // } else { - // Filemanager(".").createfile() - // } + // Optional Regex Check for a specific argument format: + MessageUtils.verifyFilename(filename, Regex("^.*[.](md|txt)$")) + // if (fileExists) { + // Filemanager(".").editfile() + // } else { + // Filemanager(".").createfile() + // } } else { if (args[0] != "-d") { throw IllegalArgumentException("[ERROR]: First parameter must be the delete flag") @@ -61,5 +61,4 @@ fun main(args: Array<String>) { } } } - } diff --git a/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt b/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt index 8963081c83529ad20f69a3a8c5a72e61fe7d1eca..0f0e3bd365cc4f7645a5a07e41d15fd78371f4e5 100644 --- a/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt +++ b/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt @@ -1,29 +1,45 @@ package notes.multi.utilities import java.io.File -import java.io.FileInputStream -import java.util.* -import kotlin.io.path.exists -class Filemanager(val dir: String) { - private val directory = File(dir) +import java.io.InputStream - fun files() : MutableList<File> { - val retfiles = mutableListOf<File>() +class Filemanager(private val dir: String, private val name: String) { + private val directory = File(dir) + private val filepath = File("$dir/$name") + private val listfiles = mutableListOf<File>() + init { for (f in directory.listFiles()!!) { if (f.extension == "txt" || f.extension == "md") { - retfiles.add(f) + listfiles.add(f) } } - return retfiles } + //returns list of files + fun files() : MutableList<File> { + return listfiles + } + + + // create and write to that file + fun writefile(line:String) { + filepath.writeText(line) + listfiles.add(filepath) + } + + // opens and read the existing file + fun openfile(): String { + if (!listfiles.contains(filepath)) { + return "" + } + val inputStream: InputStream = filepath.inputStream() - fun createfile() { - //TODO: Create File + return inputStream.bufferedReader().use { it.readText() } } - fun deletefile(path:String) { - //TODO: DELETE FILE + fun deletefile():Boolean { + listfiles.remove(filepath) + return filepath.delete() } } \ No newline at end of file