Reputation:
I have a directory like this:
assigment
|
|__ src
| |
| |__ Main.scala
|
|
|__ testcase
|
|__ Simple.in
In Main.scala, Simple.in is read by Source.fromFile()
:
val inputFile = "Simple.in"
val lines = Source.fromFile("./testcase/" + inputFile).getLines
But when I run Main.scala in sbt
the FileNoutFoundException
appear. When I change the path to "../testcase/" + inputFile
then it works fine. The original path is from my teacher, so I wonder which path is actually correct? Oh, I'm using Linux btw...
Upvotes: 2
Views: 51
Reputation: 14825
.
=> current dir
..
=> one above curren dir
But standard way to access resources is using the resources
folder of sbt project structure.
This way helps you to access files independent of where (which class) you are accessing the resource in the code.
Folder to put your files
src/main/resources
val stream : InputStream = getClass.getResourceAsStream("/readme.txt")
val lines = scala.io.Source.fromInputStream( stream ).getLines
Upvotes: 1
Reputation: 140427
./ means: the current path
../ means: the directory "above" the current directory
Thus: when you run your Scala class from "src", "./testcase" makes it look for a directory testcase within "src"; or using full path names:
"assignment/src/" + "./testcase" turns into "assignment/src/testcase"
Whereas, when you use
"assignment/src/" + "../testcase" turns into "assignment/testcase"
therefore, the version with ".." finds the valid path. That is all the magic here!
Upvotes: 1