4

I am trying to access a file in Scala using io.Source.fromfile.
I have specified the full path, but i am still getting a no such directory or file error.

This is a general version of what my code looks like:

val lines = io.Source.fromFile("~/top/next/source/resources/desiredFile.txt").getLines()

I'm running Ubuntu if that makes any difference.

2 Answers 2

7

It probably because you are using tilde sign, use full absolute path. If you want to avoid hard coding your home directory, you can get it from environment variables:

val home = System.getProperty("user.home")
val s = Source.fromFile(s"${home}/.....").getLines() 
Sign up to request clarification or add additional context in comments.

1 Comment

meaning what exactly? Should I specify home/ instead of ~/?
5

The compiler was assuming it should start in the project folder I was already in, so when I specified the directories above that folder, it tried to find them all as a sub-directory of my root project folder.
This would obviously cause an error.

I now have:

val lines = io.Source.fromFile("source/resources/desiredFile.txt")

which is working properly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.