有没有一种很好、安全、快速的方法来将 InputStream 写入 Scala 中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2782638/
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 02:07:54  来源:igfitidea点击:

Is there a nice, safe, quick way to write an InputStream to a File in Scala?

javafilescalainputstreamlift

提问by pr1001

Specifically, I'm saving a file upload to local file in a Lift web app.

具体来说,我将文件上传保存到 Lift Web 应用程序中的本地文件。

采纳答案by Rex Kerr

If it's a text file, and you want to limit yourself to Scala and Java, then using scala.io.Sourceto do the reading is probably the fastest--it's not built in, but easy to write:

如果它是一个文本文件,并且您想将自己限制在 Scala 和 Java,那么scala.io.Source用于读取可能是最快的——它不是内置的,但易于编写:

def inputToFile(is: java.io.InputStream, f: java.io.File) {
  val in = scala.io.Source.fromInputStream(is)
  val out = new java.io.PrintWriter(f)
  try { in.getLines().foreach(out.println(_)) }
  finally { out.close }
}

But if you need other libraries anyway, you can make your life even easier by using them (as Michel illustrates).

但是,如果您无论如何都需要其他库,则可以通过使用它们使您的生活更加轻松(如 Michel 所示)。

(P.S.--in Scala 2.7, getLinesshould not have a ()after it.)

(PS--在 Scala 2.7 中,后面getLines不应该有()。)

(P.P.S.--in old versions of Scala, getLinesdid not remove the newline, so you need to printinstead of println.)

(PPS——在旧版本的 Scala 中,getLines没有删除换行符,所以你需要print代替println.)

回答by notan3xit

With Java 7 or later you can use Filesfrom the new File I/O:

使用 Java 7 或更高版本,您可以Files从新的 File I/O 使用

Files.copy(from, to)

where fromand tocan be Paths or InputStreams. This way, you can even use it to conveniently extract resources from applications packed in a jar.

其中fromto可以是Paths 或InputStreams。这样,您甚至可以使用它从打包在 jar 中的应用程序中方便地提取资源。

回答by Michel Kr?mer

I don't know about any Scala specific API, but since Scala is fully compatible to Java you can use any other library like Apache Commons IOand Apache Commons FileUpload.

我不知道任何特定于 Scala 的 API,但由于 Scala 与 Java 完全兼容,因此您可以使用任何其他库,如Apache Commons IOApache Commons FileUpload

Here is some example code (untested):

这是一些示例代码(未经测试):

//using Commons IO:
val is = ... //input stream you want to write to a file
val os = new FileOutputStream("out.txt")
org.apache.commons.io.IOUtils.copy(is, os)
os.close()

//using Commons FileUpload
import javax.servlet.http.HttpServletRequest
import org.apache.commons.fileupload.{FileItemFactory, FileItem}
import apache.commons.fileupload.disk.DiskFileItemFactory
import org.apache.commons.fileupload.servlet.ServletFileUpload
val request: HttpServletRequest = ... //your HTTP request
val factory: FileItemFactory = new DiskFileItemFactory()
val upload = new ServletFileUpload(factory)
val items = upload.parseRequest(request).asInstanceOf[java.util.List[FileItem]]
for (item <- items) item.write(new File(item.getName))

回答by phidias

The inputToFile method given above doesn't work well with binary files like .pdf files. It throws a runtime exception while attempting to decode the file into string. What worked for me was this:

上面给出的 inputToFile 方法不适用于 .pdf 文件等二进制文件。它在尝试将文件解码为字符串时抛出运行时异常。对我有用的是:

def inputStreamToFile(inputStream: java.io.InputStream, file: java.io.File) = {
    val fos = new java.io.FileOutputStream(file)
    fos.write(
      Stream.continually(inputStream.read).takeWhile(-1 !=).map(_.toByte).toArray
    )
    fos.close()
}