loading...

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

itscoderslife profile image Damodar Shenoy ・1 min read

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!")

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")
}

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")
}

Posted on by:

itscoderslife profile

Damodar Shenoy

@itscoderslife

Mobile App Developer. Working as iOS developer with Obj-C / Swift. Learning Android development. Full-stack enthusiast. All things tech. Exploring Machine Learning.

Discussion

markdown guide