Kotlin Program to Create String from Contents of a File

Kotlin Program to Create String from Contents of a File. Kotlin has inherited Java’s fidgety (but very flexible) way of doing I/O, but with some simplifying extra features.

Kotlin Program to Create String from Contents of a File

Source Code

import java.io.File
 
fun main(args: Array<String>) {
 
    val fileName = "data.txt"
 
    var file = File(fileName)
 
    // create a new file
    val isNewFileCreated :Boolean = file.createNewFile()
 
    if(isNewFileCreated){
        println("$fileName is created successfully.")
    } else{
        println("$fileName already exists.")
    }
 
    // try creating a file that already exists
    val isFileCreated :Boolean = file.createNewFile()
 
    if(isFileCreated){
        println("$fileName is created successfully.")
    } else{
        println("$fileName already exists.")
    }
 
}

Output