scala 如何从Scala中的文件中获取第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8865434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:48:23  来源:igfitidea点击:

How to get first line from file in Scala

parsingscalacsv

提问by dave

I'd like to get just the first line from a CSV file in Scala, how would I go about doing that without using getLine(0) (it's deprecated)?

我只想从 Scala 中的 CSV 文件中获取第一行,如果不使用 getLine(0)(已弃用),我将如何执行此操作?

回答by ziggystar

If you don't care about releasing the file resource after using it, the following is a very convienient way:

如果你不关心使用后释放文件资源,下面是一个非常方便的方式:

Source.fromFile("myfile.csv").getLines.next()

Source.fromFile("myfile.csv").getLines.next()

回答by Rex Kerr

If you want to close the file, and you want to get an empty collection rather than throw an error if the file is actually empty, then

如果你想关闭文件,并且你想得到一个空的集合而不是在文件实际上是空的情况下抛出错误,那么

val myLine = {
  val src = Source.fromFile("myfile.csv")
  val line = src.getLines.take(1).toList
  src.close
  line
}

is about the shortest way you can do it if you restrict yourself to the standard library.

如果您将自己限制在标准库中,那么这是您可以做到的最短方法。

回答by leedm777

FWIW, here's what I would do (sticking w/ the standard library):

FWIW,这就是我要做的(坚持使用标准库):

def firstLine(f: java.io.File): Option[String] = {
  val src = io.Source.fromFile(f)
  try {
    src.getLines.find(_ => true)
  } finally {
    src.close()
  }
}

Things to note:

注意事项:

  1. The function returns Option[String]instead of List[String], since it always returns one or none. That's more idiomatic Scala.
  2. The srcis properly closed, even in the very off chance that you could open the file, but reading throws an exception
  3. Using .find(_ => true)to get the first item of the Iteratordoesn't make me feel great, but there's no nextOptionmethod, and this is better than converting to an intermediate Listyou don't use.
  4. IOExceptions opening or reading the file are passed along.
  1. 该函数返回Option[String]而不是List[String],因为它总是返回 1 或无。那是更惯用的 Scala。
  2. src关好,即使是在非常起飞的机会,你可以打开该文件,但阅读抛出一个异常
  3. 使用.find(_ => true)来获得第一项Iterator并没有让我感觉很好,但是没有nextOption方法,这比转换为List您不使用的中间件要好。
  4. IOExceptions 打开或读取文件被传递。

I also recommend using the scala-armlibrary to give you a better API for managing resources and automagically closing files when you need to.

我还建议使用scala-arm库为您提供更好的 API 来管理资源并在需要时自动关闭文件。

import resource._

def firstLine(f: java.io.File): Option[String] = {
  managed(io.Source.fromFile(f)) acquireAndGet { src =>
    src.getLines.find(_ => true)
  }
}

回答by gieldops

I think all other solutions either read in all lines and then only retain the first line, or don't close the file. A solution which doesn't have these problems is:

我认为所有其他解决方案要么读入所有行,然后只保留第一行,要么不关闭文件。没有这些问题的解决方案是:

val firstLine = {
  val inputFile = Source.fromFile(inputPath)
  val line = inputFile.bufferedReader.readLine
  inputFile.close
  line
}

I only have 1 week of experience with Scala so correct me if I'm wrong.

我只有 1 周的 Scala 经验,所以如果我错了,请纠正我。