Kotlin – How to read File with Kotlin language

This tutorial shows you how to read File in Kotlin using InputStream or BufferedReader.

I. Technology

– Java 1.8
– Kotlin 1.1.2

II. Overview

1. Goal

Read file: all lines/by line using InputStream or BufferedReader or File directly.

2. Steps to do

– Create:
+ InputStream from File, then get BufferedReader using bufferedReader() method
+ BufferedReader from File.
– Use:
+ Closeable.use() method with Reader.readText() method inside block. Closeable.use() will automatically close the input at the end of the lambda’s execution:


String = (Reader implements Closeable).use(Reader.readText())

+ Reader.useLines() method with Kotlin Sequence (a sequence of all the lines) inside block. Reader.useLines() will automatically close the reader once the processing is complete:


Reader.useLines(block: Sequence)

+ File.useLines() method with Kotlin Sequence (a sequence of all the lines) inside block. It will close the reader once the processing is complete:


File.useLines(block: Sequence)

+ File.readLines() method to return a List<String>:


File.readLines()

III. Practice

0. kotlin.txt


Kotlin
-> Be Kotlineer
-> Be Simple
-> Be Connective

1. Use InputStream

1.1 Read All Lines

import java.io.File
import java.io.InputStream

fun main(args: Array) {
	val inputStream: InputStream = File("kotlin.txt").inputStream()

	val inputString = inputStream.bufferedReader().use { it.readText() }
	println(inputString)
}

Check Result:


Kotlin
-> Be Kotlineer
-> Be Simple
-> Be Connective
1.2 Read By Line

import java.io.File
import java.io.InputStream

fun main(args: Array) {
	val inputStream: InputStream = File("kotlin.txt").inputStream()
	val lineList = mutableListOf()

	inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }
	lineList.forEach{println(">  " + it)}
}

Check Result:


>  Kotlin
>  -> Be Kotlineer
>  -> Be Simple
>  -> Be Connective

2. Use BufferedReader

2.1 Read All Lines

import java.io.File
import java.io.BufferedReader

fun main(args: Array) {
	val bufferedReader: BufferedReader = File("kotlin.txt").bufferedReader()

	val inputString = bufferedReader.use { it.readText() }
	println(inputString)
}

Check Result:

Kotlin
-> Be Kotlineer
-> Be Simple
-> Be Connective
2.2 Read By Line
import java.io.File
import java.io.BufferedReader

fun main(args: Array) {
	val bufferedReader = File("kotlin.txt").bufferedReader()
	val lineList = mutableListOf()

	bufferedReader.useLines { lines -> lines.forEach { lineList.add(it) } }
	lineList.forEach { println(">  " + it) }
}

Check Result:


>  Kotlin
>  -> Be Kotlineer
>  -> Be Simple
>  -> Be Connective

3. Use File directly


import java.io.File
import java.io.BufferedReader

fun main(args: Array) {
	val lineList = mutableListOf()

	File("kotlin.txt").useLines { lines -> lines.forEach { lineList.add(it) }}
	lineList.forEach { println(">  " + it) }

//	val lineList = File("kotlin.txt").readLines()
//	lineList.forEach { println(">  " + it) }

Check Result:


>  Kotlin
>  -> Be Kotlineer
>  -> Be Simple
>  -> Be Connective


By grokonez | August 29, 2017.

Last updated on May 7, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*