如何在Scala中将文件读取为字节数组 [英] How to read a file as a byte array in Scala

查看:787
本文介绍了如何在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.

这篇关于如何在Scala中将文件读取为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆