DEV Community

Coder
Coder

Posted on

2 1

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay