How to read a file as a byte array in Scala

前端 未结 7 1230
情话喂你
情话喂你 2020-12-02 08:31

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

I just want to read in some file and get a byte

7条回答
  •  旧时难觅i
    2020-12-02 09:13

    I have used below code to read a CSV file.

    import scala.io.StdIn.readLine
    import scala.io.Source.fromFile
    
    readFile("C:/users/xxxx/Downloads/", "39025968_ccccc_1009.csv")
    
    def readFile(loc :String,filenm :String): Unit ={
    
      var flnm = fromFile(s"$loc$filenm") // Imported fromFile package
    
      println("Files testing")
      /*for (line <- flnm.getLines()) {
        printf("%4d %s\n", line.length, line)
      }*/
      flnm.getLines().foreach(println) // getLines() is imported from readLines.
      flnm.close() 
    }
    

提交回复
热议问题