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

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

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

Okay