Skip to content

Instantly share code, notes, and snippets.

@sahildev001
Created March 29, 2023 11:21
Show Gist options
  • Save sahildev001/f546643376dc2eb7ac0cfd2a4a5a4e6b to your computer and use it in GitHub Desktop.
Save sahildev001/f546643376dc2eb7ac0cfd2a4a5a4e6b to your computer and use it in GitHub Desktop.
Kotlin function to make list from directory files
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