Kotlin Read File – 6 Different Ways
Kotlin Read File – We can read the contents of a file in Kotlin either by using standard methods of the java.io.File class, or the methods that Kotlin provides as an extension to java.io.File.
We shall look into example programs for the extension methods, provided by Kotlin to Java‘s java.io.File Class, to read the contents of a file.
Example programs to read contents of a file in Kotlin
- File.bufferedReader() : To read contents of a file into BufferedReader
- File.forEachLine() : To read a file line by line in Kotlin
- File.inputStream() : To read contents of file to InputStream
- File.readBytes() : To read contents of file to ByteArray
- File.readLines() : To read lines in file to List of String
- File.readText() : To read contents of file to a single String
File.bufferedReader() : How to read contents of a file into BufferedReader
- Prepare file object with the location of the file passed as argument to File class constructor.
- File.bufferedReader returns a new BufferedReader for reading the content of the file.
- Use BufferedReader.readLines() to read the content of the file.
Kotlin Program – example.kt
import java.io.File
/**
* Created by www.tutorialkart.com
* Example program to read contents of a file in Kotlin into BufferedReader
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
val bufferedReader = file.bufferedReader()
val text:List<String> = bufferedReader.readLines()
for(line in text){
println(line)
}
}
Output
Good Day !
Welcome to Kotlin Tutorials in www.tutorialkart.com.
Learn Kotlin easily here.
Happy learning.
Process finished with exit code 0
Content of file is printed to the console.
File.forEachLine() : Read a file line by line in Kotlin
- Prepare file object with the location passed as argument to File constructor.
- Use File.forEachLine function and read each line of the file.
Kotlin Program – example.kt
import java.io.File
/**
* Created by www.tutorialkart.com
* Example program to read contents of a file in Kotlin line by line
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
file.forEachLine { println(it) }
}
Output
Good Day !
Welcome to Kotlin Tutorials in www.tutorialkart.com.
Learn Kotlin easily here.
Happy learning Kotlin.
Process finished with exit code 0
Content of file is printed to the console.
File.inputStream() : Read contents of file to InputStream
- Prepare file object with the location of the file passed as argument to File class constructor.
- File.inputStream() returns a new InputStream for the file.
- Then you can read bytes from the stream and convert it to a String. This string is content of the file.
Read Content of File to InputStream and then to a String
Kotlin Program – example.kt
import java.io.File
import java.io.InputStream
/**
* Created by www.tutorialkart.com
* Example program to read contents of a file in Kotlin to InputStream
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
var ins:InputStream = file.inputStream()
// read contents of IntputStream to String
var content = ins.readBytes().toString(Charset.defaultCharset())
println(content)
}
Content of file is printed to the console.
File.readBytes() : returns entire content of file as a ByteArray
- Prepare file object with the location passed as argument to File constructor.
- Use File.readBytes() function to get ByteArray. This byte array contains all the file contents.
- Use for loop to iterate over the byte array and access the contents of file byte by byte.
Kotlin Program – example.kt
import java.io.File
/**
* Created by www.tutorialkart.com
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
var bytes:ByteArray = file.readBytes()
for(byte in bytes){
print(byte.toChar())
}
}
Content of file is printed to the console.
File.readLines() : returns entire content of file as a List of lines
- Prepare file object with the location passed as argument to File constructor.
- Use File.readLines() function to get all the lines in the text file as List of Strings.
- You can access the list using for loop to read each line of the file.
Kotlin Program – example.kt
import java.io.File
/**
* Created by www.tutorialkart.com
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
var lines:List<String> = file.readLines()
for(line in lines){
println(line)
}
}
Content of file is printed to the console.
File.readText() : returns entire content of file as a single string
You can also read whole content of the file as a single string. Follow these steps.
- Prepare file object with the location passed as argument to File constructor.
- Use File.readText() function that returns entire content of file as a String.
Kotlin Program – example.kt
import java.io.File
/**
* Created by www.tutorialkart.com
*/
fun main(args: Array<String>) {
val file = File("input"+File.separator+"contents.txt")
var content:String = file.readText()
println(content)
}
Content of file is printed to the console.
Conclusion
In this Kotlin Tutorial – Kotlin Read File, we have learned to read the contents of a file in Kotlin, read a file to a byte array, read a file to InputStream, read a file to list of strings, read a file line by line, read a file to BufferedReader.