📜  在 Kotlin 中使用 InputReader 读取文件

📅  最后修改于: 2022-05-13 01:54:22.594000             🧑  作者: Mango

在 Kotlin 中使用 InputReader 读取文件

基本上,kotlin.io 提供了一个不错的、干净的 API 用于读取和写入文件。在本文中,我们将讨论如何在 Kotlin 中使用inputReader读取文件。一种方法是使用inputreader 。我们将在本文中看到如何做到这一点。

例子

有很多方法可以从文件中读取,但了解它们背后的动机非常重要,这样才能为我们的目的使用正确的方法。首先,我们将尝试获取文件的 Input Stream 并使用 reader 读取内容:

Kotlin
import java. io. File
import java . io. InputStream
fun main (args: Array) {
  val inputStream: InputStream = File ("gfg.txt").inputStream()
  val inputString = inputStream.reader().use {it.readText()}
  println (inputString)
}


Kotlin
import java.io.File
fun main (args: Array) {
  val inputString = File("gfg.txt").reader().use{it.readText()}
  println (inputString)
}


Kotlin
import java.io.File
import java.io.InputStream
  
fun main (args: Array) {
  val listOfLines = mutableListof ()
  val inputStream: InputStream = File ("gfg.txt").inputStream()
  inputStream.reader().useLines { lines -> lines.forEach {
    listOfLines.add (it) 
  }
    listOfLines.forEach{println("$ "+ it )} 
 }


Kotlin
import java.io.File
fun main (args: Array){
  val listOfLines = mutableListOf()
    
  File ("gfg.txt").reader().useLines{ lines ->lines.forEach {
    listOfLines.add (it) }
  }
  listOfLines.forEach{println("$ " + it}
}


在前面的代码块中,gfg.txt 只是我们要读取的文件。该文件与我们的代码源文件位于同一文件夹中。如果我们需要读取位于不同文件夹中的文件,它看起来类似于以下内容:

File ("/path/to/file/gfg.txt")

这段代码只是获取文件中的所有文本并将其打印在控制台上。另一种读取文件内容的方法是直接创建文件的阅读器,就像我们在这段代码中所做的那样:

科特林

import java.io.File
fun main (args: Array) {
  val inputString = File("gfg.txt").reader().use{it.readText()}
  println (inputString)
}

前面两个代码块的输出将只是文件中的文本。在我们的例子中,它是这样的:

输出:

GeeksforGeeks.org was created with a goal in mind to provide well written, well thought and well explained solutions for selected questions.
The core team of five super geeks constituting of technology lovers and computer science enthusiasts have been constantly working in this direction.
The content on GeeksforGeeks has been divided into various categories to make it easily accessible for the users.
Whether you want to learn algorithms, data structures or it is the programming language on it's own which interests you.
GeeksforGeeks has covered everything. 
Even if you are looking for Interview preparation material. 
GeeksforGeeks has a vast set of company-wise interview experiences to learn from.
that gives a user insights into the recruitment procedure of a company. 
Adding to this, it serves as a perfect platform for users to share their knowledge via contribute option.

现在,如果我们想逐行读取文件,因为我们想对每一行进行一些处理,该怎么办?在这种情况下,我们使用useLines()方法代替use()。查看以下示例,我们从文件中获取输入流并使用useLines () 方法逐行获取每一行:

科特林

import java.io.File
import java.io.InputStream
  
fun main (args: Array) {
  val listOfLines = mutableListof ()
  val inputStream: InputStream = File ("gfg.txt").inputStream()
  inputStream.reader().useLines { lines -> lines.forEach {
    listOfLines.add (it) 
  }
    listOfLines.forEach{println("$ "+ it )} 
 }

或者,如果我们希望直接在文件上使用阅读器,我们这样做:

科特林

import java.io.File
fun main (args: Array){
  val listOfLines = mutableListOf()
    
  File ("gfg.txt").reader().useLines{ lines ->lines.forEach {
    listOfLines.add (it) }
  }
  listOfLines.forEach{println("$ " + it}
}

在这种情况下,输出将如下所示:

输出:

$ GeeksforGeeks.org was created with a goal in mind to provide well written, well thought and well explained solutions for selected questions.
$ The core team of five super geeks constituting of technology lovers and computer science enthusiasts have been constantly working in this direction.
$ The content on GeeksforGeeks has been divided into various categories to make it easily accessible for the users.
$ Whether you want to learn algorithms, data structures or it is the programming language on it's own which interests you.
$ GeeksforGeeks has covered everything.
$ Even if you are looking for Interview preparation material.
$ GeeksforGeeks has a vast set of company-wise interview experiences to learn from.
$ that gives a user insights into the recruitment procedure of a company.
$ Adding to this, it serves as a perfect platform for users to share their knowledge via contribute option.

您是否注意到我们使用use()useLines()方法来读取文件?对 Closeable 的调用。 use()函数将在 lambda 执行结束时自动关闭输入。现在,我们当然可以使用Reader.readText () ,但这不会在执行后关闭流。除了use () 之外,还有其他方法,例如Reader.readText()等,可以用来读取流或文件的内容。使用任何方法的决定取决于我们是否希望流在执行后自行关闭,或者我们想要处理关闭资源,以及我们是想要从流中读取还是直接从文件中读取。

BufferedReader 一次从输入流中读取几个字符并将它们存储在缓冲区中。这就是它被称为 BufferedReader 的原因。另一方面,输入读取器仅从输入流中读取一个字符,其余字符仍保留在流中。在这种情况下没有缓冲区。这就是 BufferedReader 速度快的原因,因为它维护一个缓冲区,并且与从磁盘中检索数据相比,从缓冲区中检索数据总是更快。