• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding, Scala IO and reading a file

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic