In this recipe, we are going to retrieve the contents of a file as text and print it to the console. We are going to use the standard library File.readText() extension function, returning a String representing the text content of the given File instance.
Reading the contents of a file
Getting ready
Make sure you have a sample non-empty file included in your project to read its contents. You can clone the sample project provided with the book's GitHub repository: https://github.com/PacktPublishing/Kotlin-Standard-Library-Cookbook. In this recipe, we are going to use the file1.txt file located in the src/main/resources directory in the sample project.