DEV Community

Coder
Coder

Posted on

Android-Kotlin Digest #4 : A simple way to Read and Write

Here I will give you an overview of :

  • how to read user input from command line?

  • how to read data from a file?

  • how to write to a file?

How to read user input from command line?

println(" ->Welcome to our Bakery<-")

println("============================")


println("Hello! How can I help you?")

var userResponse = readLine()


println("Sure!!" + "It's fresh prepared just now!")

var userNextResponse = readLine()


println("You are very welcome! Have a nice day!")



println("================== ====================")


println(" :: Your Conversation with the Clerk :: ")

println("Clerk: Hello! How can I help you?")

println("Customer: $userResponse")

println("Clerk: Sure!! It's fresh prepared just now!")

println("Customer: $userNextResponse")

println("Clerk: You are very welcome! Have a nice day!")
Enter fullscreen mode Exit fullscreen mode

How to read data from a file?

val reader = FileReader("itsdamslife.txt")
var char: Int?
try {
    do {
        char = reader.read()
        print(char.toChar())
    } while (char != -1)
} catch (exception: Exception) {
    print("There was an exception : $exception.message")
}
Enter fullscreen mode Exit fullscreen mode

How to write to a file?

try {

    var writer = FileWriter("itsdamslife.txt", true)
    writer.write(message + "\n")
    writer.close()
} catch (exception: Exception) {
    println("There was an exception :  $exception")
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)