如何在Scala中将文件作为字节数组读取

如何在Scala中将文件作为字节数组读取

问题描述:

我可以找到大量示例,但它们似乎要么主要依赖 Java 库,要么只是读取字符/行/等.

I can find tons of examples but they seem to either rely mostly on Java libraries or just read characters/lines/etc.

我只想读入一些文件并获取一个带有 Scala 库的字节数组 - 有人可以帮助我吗?

I just want to read in some file and get a byte array with scala libraries - can someone help me with that?

Java 7:

import java.nio.file.{Files, Paths}

val byteArray = Files.readAllBytes(Paths.get("/path/to/file"))

我相信这是最简单的方法.只是利用这里的现有工具.NIO.2很棒.

I believe this is the simplest way possible. Just leveraging existing tools here. NIO.2 is wonderful.