Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Trouble getting a resource when It's In a folder.

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I have a text document In com\pacman\menu.txt . I can access It fine when I use:



If I create a directory and place the document In there as com\pacman\Menu\menu.txt , I can no longer access the document.
I've tried:

(It's a windows machine).
and


I'm not sure what I'm doing wrong here.
 
Ranch Hand
Posts: 146
2
Mac Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what the leading bracket is doing directly after the assignment operator. Also, I've never seen a stream accessed that way before. I'd recommend using a BufferedReader with a FileReader, see here. Alternatively, give java.nio.Files and Paths a try.

However, if you want to keep with the current implementation it might be worth trying the absolute path to the file. Perhaps you could programmatically determine the current directory so you don't have to keep changing the path to Menu.txt if you move the program to a different location in your file system.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Thanks for replying.
That bracket was an error.
I'll try the absolute path.

It's not the whole code. The InputStream Is chained to an InputStreamReader and BufferedReader. I Just left that out.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that works, if not, I'll be scratching my head too.
 
Marshal
Posts: 75637
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the file is in \com\pacman then the resource you are looking for in the first code block does not exist, and it is hardly surprising you can't find it.
All such paths are relative paths from the root of the folder structure you are working in, but I can never seem to remember where the root is, so try from the folder which contains com as a subdirectory and see what happens.
 
Saloon Keeper
Posts: 25456
178
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though this is a Windows system, I still recommend using the platform-neutral path specification. In other words:


This makes the code more portable, less likely to get zapped by mislaid backslashes and...

Unless I am mistaken, the getResourceAsStream method's idea of "absolute path" is a Java path relative to the classpath base. So in other words, if I have a file named "C:/Program Files/MyJava/App1/classes/com/javaranch/saloon/images/pic1.jpg", the resource "absolute path" would be "/com/javaranch/saloon/images/pic1.jpg".
 
Campbell Ritchie
Marshal
Posts: 75637
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . if I have a file named "C:/Program Files/MyJava/App1/classes/com/javaranch/saloon/images/pic1.jpg", the resource "absolute path" would be "/com/javaranch/saloon/images/pic1.jpg".

And presumably you would have classes as your current directory?
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's fine now. Thanks for all the Info. That bracket was doing strange things !
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic