2015-11-17 67 views
2

我只想讀取資源中的文件並獲得一個字節數組?有人可以幫助我嗎?如何將資源文件讀入scala中的字節數組?

+1

幾乎重複http://stackoverflow.com/questions/27360977/how-to-read-files-from-resources -folder-in-scala?rq = 1(它試圖讀取行而不是字節,但也許你可以適應它,例如通過將它與http://stackoverflow.com/questions/7598135/how-to-讀一個文件作爲一種字節陣列合階) – Thilo

回答

2

How to read a file as a byte array in Scala描述,下面的代碼片段應該做的伎倆:

def slurp(resource: String) = { 
    val bis = new BufferedInputStream(getClass.getResource(resource)) 
    try Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray 
    finally bis.close() 
} 
相關問題