score:-1
I'm able to get the output by putting dummy.txt
in src/test/resources
and running a test case.
println(Source.fromResource("dummy.txt").getLines.length)
score:11
Use getClass
not classLoader
and put /
in front of the resource name /war-and-peace.txt
. war-and-peace.txt
file must be in resources folder (src/main/resources).
val file = getClass.getResource("/war-and-peace.txt").getFile()
Source.fromFile(file).getLines.foreach(println)
in one line
Source.fromFile(getClass.getResource("/war-and-peace.txt").getFile).getLines().foreach(println)
getClass.getResourceAsStream is more reliable as it works when the code is packaged inside a jar file also
Source.fromInputStream(getClass.getResourceAsStream("/war-and-peace.txt")).getLines().foreach(println)
Source: stackoverflow.com
Related Query
- Scala does not read file from resources folder
- Simple file read with Scala does not work
- scala typesafe config - How to load conf file from classpath not from top level resources
- read .html file from resources folder on server in spring boot
- Scala Eclipse project - unable to read text file from resources directory
- How to read files from resources folder in Scala?
- Scala get file path of file in resources folder
- Read a matrix from a file in Scala Breeze
- Is it possible to implement `??` (a null coalescing operator from C#) in Scala that does not use reflection?
- how to read immutable data structures from file in scala
- Spark load csv file in jar from resources folder
- Renaming a .scala file in Scala IDE does not rename the class
- Scala - Remove files from folder by file suffix name
- Spark: read csv file from s3 using scala
- Move file from one folder to another on HDFS in Scala / Spark
- How to read HDFS file from Scala code
- scala issue with reading file from resources directory
- Read JSON files from multiple line file in spark scala
- How to read a file in resources folder as an InputStream in Scala?
- Not able to read conf file in spark scala
- How to get load file under resources folder in scala sbt
- Scala - mutable HashMap -> write to file / read from file -> HashMap null
- How to list all files from resources folder with scala
- Scala dynamic String interpolation - Read from properties file
- Spark streaming does not read files moved from HDFS to HDFS
- Why does scala copy not preserve state from original class instance?
- How the Scala script that reads 5G log file from network drive should be modified in order to read last x lines (like 'tail' in Unix)?
- How to read a text file from HDFS in Scala natively (without using Spark)?
- why scala assembly include application.conf file from test folder in jar?
- How to read file from Blob storage using scala to spark
More Query from same tag
- Scala simple way to search timeStamps in a Map of timestamps
- To add rolling window function in scala with multiple filters
- Window function with partitionBy for Spark data frame is not working
- What is the preferred way to deal with two-dimensional arrays?
- Mixing object-oriented and functional programming
- What is the difference between private modifier position in a Scala class declaration?
- Scala parameter value does not compare correctly - checking for empty string value with if statement in view
- Play 2.2 for Scala - Modifying AcceptLanguage HTTP Request Header with ScalaInterceptors
- Scala Option(null) expected as None but I got Some(0)
- type parameters and numeric widening
- Unable to reproduce Monoid examples from shapeless
- Add scala test-case to jar file in maven project
- org.mockito.exceptions.misusing.WrongTypeOfReturnValue on spark test cases
- It is possible to define Scala case objects programmatically?
- How to set array of records Using GenericRecordBuilder
- scalaxb generate XML element with attributes
- OSGi Karaf Scala UnsupportedAudioFileException
- How do I create an enum in scala that has an extra field
- In Apache Spark, how to convert a slow RDD/dataset into a stream?
- ActorReferences as member variables in other actors in Akka
- Scala Connect Four Moving Between Rows
- Slick 1.0.1 batch insert how to ignore error and return success entries
- Important Scala concepts to "be thorough in" when learning Lift
- How to load csv couple of lines per couple of lines
- serializing and deserializing a json object through Lift-JSON
- How to join two Hbase tables in Spark using Scala
- how can I pass KafkaAvroSerializer into a Kafka ProducerRecord?
- Scala's MapLike, ListLike, SeqLike, etc how does each compare to Map, List, Seq?
- Scala split function error
- Verifying that a Datomic entity id is valid