Created
March 29, 2023 11:21
-
-
Save sahildev001/f546643376dc2eb7ac0cfd2a4a5a4e6b to your computer and use it in GitHub Desktop.
Kotlin function to make list from directory files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun listFilesInDirectory(directoryPath: String): List<File> { | |
val directory = File(directoryPath) | |
return directory.listFiles()?.toList() ?: emptyList() | |
} | |
//--- Implement Read text from file | |
fun readTextFromFile(filePath: String): String? { | |
val file = File(filePath) | |
if (!file.exists() || !file.isFile) { | |
return null | |
} | |
val stringBuilder = StringBuilder() | |
BufferedReader(FileReader(file)).use { reader -> | |
var line = reader.readLine() | |
while (line != null) { | |
stringBuilder.append(line+ "\n") | |
line = reader.readLine() | |
} | |
} | |
return stringBuilder.toString() | |
} | |
// use this as | |
var filepath:String = intent.getStringExtra("filepath")!! | |
var content = readTextFromFile(filepath) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment