• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • 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: 140
2
Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 140
2
Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that works, if not, I'll be scratching my head too.
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 18800
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
It's fine now. Thanks for all the Info. That bracket was doing strange things !
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!