Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding, Scala IO and reading a file

 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code



..and given that "test.txt" is in the same folder/package/location as the generated .scala file. I recieve the following error: (I have hashed out the package names as they are private company ones)



...however, if I paste the full path to the location of "test.txt" the application works. This is certainly not practical, by any means. I am using Eclipse, could it be an IDE issue, or should I be able to avoid providing the entire path to a file if its in the root location??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know if its better to use an absolute or relative path for the files. But I think using an absolute path for the file should be better. Or using some file chooser UI would also be better.
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure your problem does not lie with Scala. To test this, run Scala's REPL and try to use Source.fromFile on files in the directory from which you started the REPL. I did this and the file loaded fine using relative paths.

If you don't know what the REPL is, you should read up on it. This is a very nice feature of Scala and many other languages.

Anyhow, I'm not sure how you are using Eclipse, but I think you need "test.txt" in the directory that serves as the base when running your application.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Davies wrote:
..and given that "test.txt" is in the same folder/package/location as the generated .scala file. I recieve the following error: (I have hashed out the package names as they are private company ones)

[CODE]
Exception in thread "main" java.lang.ExceptionInInitializerError
#######.main.FileProcessorUtilityTest.main(FileProcessorUtility.scala)
Caused by: java.io.FileNotFoundException: test.txt (No such file or directory)


The location of the scala file, as well as the location of the class-File, is of no interest at all.

Think about cp, ls, less and so on. You nearly never switch to /usr/bin, to invoke those commands/programs. They are shell-builtins? Take xpdf, oowriter, eog, gimp, ...

Your current position is of interest, and the position of the file in question ("test.txt"). In the shell, the first thing is easy to control: pwd, and often shown by the prompt.

On the desktop, inside the IDE, your position is nearly invisible. Eclipse has some settings in the 'run as ...' - Dialog. Normally that's the bin or classes-dir. While the test.txt, if it resides where the source is, probably is ("../src/test.txt").

Your scala-file is generated?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic