This tutorial shows you how to read File in Kotlin using InputStream
or BufferedReader
.
>>> Refer to: JavaSampleApproach.com
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:
1 |
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:
1 |
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:
1 |
File.useLines(block: Sequence) |
+ File.readLines()
method to return a List<String>
:
1 |
File.readLines() |
III. Practice
0. kotlination.txt
1 2 3 4 |
Kotlination -> Be Kotlineer -> Be Simple -> Be Connective |
1. Use InputStream
1.1 Read All Lines
1 2 3 4 5 6 7 8 9 |
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("kotlination.txt").inputStream() val inputString = inputStream.bufferedReader().use { it.readText() } println(inputString) } |
Check Result:
1 2 3 4 |
Kotlination -> Be Kotlineer -> Be Simple -> Be Connective |
1.2 Read By Line
1 2 3 4 5 6 7 8 9 10 |
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("kotlination.txt").inputStream() val lineList = mutableListOf<String>() inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} } lineList.forEach{println("> " + it)} } |
Check Result:
1 2 3 4 |
> Kotlination > -> Be Kotlineer > -> Be Simple > -> Be Connective |
2. Use BufferedReader
2.1 Read All Lines
1 2 3 4 5 6 7 8 9 |
import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val bufferedReader: BufferedReader = File("kotlination.txt").bufferedReader() val inputString = bufferedReader.use { it.readText() } println(inputString) } |
Check Result:
1 2 3 4 |
Kotlination -> Be Kotlineer -> Be Simple -> Be Connective |
2.2 Read By Line
1 2 3 4 5 6 7 8 9 10 |
import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val bufferedReader = File("kotlination.txt").bufferedReader() val lineList = mutableListOf<String>() bufferedReader.useLines { lines -> lines.forEach { lineList.add(it) } } lineList.forEach { println("> " + it) } } |
Check Result:
1 2 3 4 |
> Kotlination > -> Be Kotlineer > -> Be Simple > -> Be Connective |
3. Use File directly
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val lineList = mutableListOf<String>() File("kotlination.txt").useLines { lines -> lines.forEach { lineList.add(it) }} lineList.forEach { println("> " + it) } // val lineList = File("kotlination.txt").readLines() // lineList.forEach { println("> " + it) } } |
Check Result:
1 2 3 4 |
> Kotlination > -> Be Kotlineer > -> Be Simple > -> Be Connective |
Milind
I have created the kotlination.txt file under resource folder. \app\src\main\res\
I am getting exception ‘no such file or directory’
Could you please help me to resolve the issue?
Kotlination
Hi Milind,
Because we don’t indicate path for kotlination.txt file, so you should put it in the root folder of the Project (the folder src is located in) 🙂
If you wanna read file in \app\src\main\res\ folder, just change the path:
File(“kotlination.txt”) -> File(“src/main/res/kotlination.txt”)
Best Regards!
Christian
You could simplify to
First you could use useLines directly on the file, and second you should not create an auxiliary collections like in Java < 8 days.
Kotlination
Hi Christian,
Thank you for your suggestion. We updated that method. But we think it should be:
Best Regards,