Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

mental block or learning malfunction when it comes to paths

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 (\\).
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic