• Post Reply Bookmark Topic Watch Topic
  • New Topic

mental block or learning malfunction when it comes to paths  RSS feed

 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the subject suggests, I'm having a problem getting my head around how to use paths to files in java programs. I am using a windows machine so a typical path to a file would be something like C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg. The following is a basic program that uses a file called Sunset.jpg.



This will only work if Sunset.jpg is in the same folder as the program source file. Sure, I can move or copy jpg files but if I just wanted to have them in one basic image folder on my computer while Java programs access them ,and not move them or have multiple copies, I think I should be able to reference them using a path. But...

...doesn't work. It just tells me that the line's full of illegal escape characters (caused by the use of the backslashes, no doubt). I'm sure my problem involves more than just the improper use of backslashes. I've tried doing several things different but to no avail. I don't know why but the more I've read regarding paths the more confused I've become. Then there're URLs...Don't get me started on them.

Could anyone point me toward a clear explanation of how to use paths in Java? I don't have any problem moving through paths on my computer normally but when it comes to doing it in Java, I'm off the path and lost in the woods.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getResource() doesn't work with file system paths. It works with paths relative to the classpath roots, or possibly relative to where the current class is under a classpath root.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check a How to Use Icons, it should provide a good background on the topic you're having issue with.
Also, backslash (\) sign is an escape character in Java. In order to have it as part of the string (absolute path, or wherever you need it) you need to put two successive backslash signs (\\).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!