📜  在 Kotlin 中使用 BufferedReader 读取文件

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

在 Kotlin 中使用 BufferedReader 读取文件

在 Kotlin 中,BufferedReader 在读入缓冲区时会存储一些字符。这使得阅读速度更快,因此效率更高。在本文中,我们将了解如何使用 BufferedReader 读取文件的内容。

例子

我们可以直接给文件附加一个BufferedReader,读取整个文件的内容,如下代码:

Kotlin
import java.do.File
import java.in.InputStream
  
fun main (args: Array) {
  val inputString = File ("example.txt").bufferedReader ().use {
    it.readText () 
  }
  println (inputString)
}


Kotlin
import java.io.File
Import java.io.InputStream
  
fun main (args: Array){
  val listOfLines = mutableListof ()
  File ("example.txt").bufferedReader().useLines {
    lines->lines.forEach{
      var x = "> (" + it.length + ") " + it;
      listOfLines.add (x)
    }
  }
  listoflines.forEach{println(it)}
}


Kotlin
import java.io.File
import java.io.InputStream
  
fun main (args: Array){
  val listofLines = mutableListof ()
  val inputStream: InputStream = File ("example.txt").inputStream()
  inputStream.bufferedReader().useLines{
    lines -> lines.forEach {
      var x = "> (" + it.length + ") " + it;
      listOfLines.add(x)
    }
  }
  listofLines.forEach{ println(it) }
}


我们还可以逐行处理我们需要的内容,以便能够单独处理每一行。在下面的代码中,我们一行一行地在开头添加一个字符,在字符之后添加字符串的长度:

科特林

import java.io.File
Import java.io.InputStream
  
fun main (args: Array){
  val listOfLines = mutableListof ()
  File ("example.txt").bufferedReader().useLines {
    lines->lines.forEach{
      var x = "> (" + it.length + ") " + it;
      listOfLines.add (x)
    }
  }
  listoflines.forEach{println(it)}
}

在前面的代码块中,我们直接将阅读器附加到文件。但是,在某些情况下,我们需要获取数据流。在这种情况下,我们可以从要读取的文件中获取输入流,然后将 BufferedReader 附加到它。在以下代码中,我们尝试使用 BufferedReader 从文件输入流中逐行读取:

科特林

import java.io.File
import java.io.InputStream
  
fun main (args: Array){
  val listofLines = mutableListof ()
  val inputStream: InputStream = File ("example.txt").inputStream()
  inputStream.bufferedReader().useLines{
    lines -> lines.forEach {
      var x = "> (" + it.length + ") " + it;
      listOfLines.add(x)
    }
  }
  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.

输出类似于文件,忽略字符集。我们还可以指定所需的字符集,例如我们在下面的代码中如果需要的话:

bufferedReader (charset).use ( it.readText () }

当我们使用前面的任何一个示例逐行执行时,我们会得到以下输出:

输出:

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

它是如何工作的?

使用 InputStream 可以帮助我们获得想要读取的文件流。我们也可以直接从文件中读取。在任何一种情况下,BufferedReader 都会在其缓冲区中保留正在读取的一些数据,以便更快地操作,与使用 InputReader 时相比,这提高了读取操作的整体效率。我们使用 use() 和/或 useLines() 方法代替 Reader.readText() 等,以便它在执行结束时自动关闭输入流,这是一种更清洁、更负责任的处理方式/O 文件。但是,如果需要,当他们想要自己处理打开和关闭流时,可以使用诸如 Reader.readText() 之类的方法。