问题
There may be various way to read plain text file in kotlin.
I want know what are the possible ways and how I can use them.
回答1:
Using BufferedReader
import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val bufferedReader: BufferedReader = File("example.txt").bufferedReader() val inputString = bufferedReader.use { it.readText() } println(inputString) }
Using InputStream
Read By Line
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("example.txt").inputStream() val lineList = mutableListOf<String>() inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} } lineList.forEach{println("> " + it)} }
Read All Lines
import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("example.txt").inputStream() val inputString = inputStream.bufferedReader().use { it.readText() } println(inputString) }
Use File directly
import java.io.File import java.io.BufferedReader fun main(args: Array<String>) { val lineList = mutableListOf<String>() File("example.txt").useLines { lines -> lines.forEach { lineList.add(it) }} lineList.forEach { println("> " + it) } }
回答2:
The answers above here are all based on Kotlin Java. Here is a Kotlin Native way to read text files:
val bufferLength = 64 * 1024
val buffer = allocArray<ByteVar>(bufferLength)
for (i in 1..count) {
val nextLine = fgets(buffer, bufferLength, file)?.toKString()
if (nextLine == null || nextLine.isEmpty()) break
val records = parseLine(nextLine, ',')
val key = records[column]
val current = keyValue[key] ?: 0
keyValue[key] = current + 1
}
fun parseLine(line: String, separator: Char) : List<String> {
val result = mutableListOf<String>()
val builder = StringBuilder()
var quotes = 0
for (ch in line) {
when {
ch == '\"' -> {
quotes++
builder.append(ch)
}
(ch == '\n') || (ch == '\r') -> {}
(ch == separator) && (quotes % 2 == 0) -> {
result.add(builder.toString())
builder.setLength(0)
}
else -> builder.append(ch)
}
}
return result
}
See: https://github.com/JetBrains/kotlin-native/blob/master/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt
回答3:
Anisuzzaman's answer lists several possibilities.
The main differences between them are in whether the file is read into memory as a single String, read into memory and split into lines, or read line-by-line.
Obviously, reading the entire file into memory in one go can take a lot more memory, so that's something to avoid unless it's really necessary. (Text files can get arbitrarily big!) So processing line-by-line with BufferedReader.useLines() is often a good approach.
The remaining differences are mostly historical. Very early versions of Java used InputStream
&c which didn't properly distinguish between characters and bytes; Reader
&c were added to correct that. Java 8 added ways to read line-by-line more efficiently using streams (e.g. Files.lines()
). And more recently, Kotlin has added its own extension functions (e.g. BufferedReader.useLines()
) which make it even simpler.
来源:https://stackoverflow.com/questions/55182578/how-to-read-plain-text-file-in-kotlin