• 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

Escape sequences

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why I am getting a '/' instead of a '\', when trying to have code sourced from this article, to read from an images sub-directory within my Eclipse IDE project:


Output
DEBUG: path =images/middle.gif
DEBUG: currentPathC:\Documents and Settings\Jon\workspace\DialogDemo\images/middle.gif ...It should be \middle.gif I guess, but in any case why do I get the error shown in the following line?
Couldn't find file: images/middle.gif

It seems that similar issues are not intended to be fixed by Sun.

Related articles include this one, and, reference is made to the escape sequences outlined in Core Java Vol I (8th Ed) P.42-43)
 
Saloon Keeper
Posts: 15524
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously because the path string passed to your method contains a forward slash instead of a backslash.

Where is the code that passes "path" to your method?
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Obviously because the path string passed to your method contains a forward slash instead of a backslash.

Where is the code that passes "path" to your method?



Right, sorry

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you considered using "images"+File.pathSeparator+"middle.gif"?
(note that the concatenation in this case will likely be managed by the compiler optimization)
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David O'Meara wrote:have you considered using "images"+File.pathSeparator+"middle.gif"?
(note that the concatenation in this case will likely be managed by the compiler optimization)



Well, I'm not sure because my File.pathSeparator returns ";", and I'm not sure whether I can change the value from the default:


Output
;
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.getResource doesn't take an absolute file path the way you are trying to do. It takes a relative path to the current class and uses / for path separator; starting with / makes it an absolute path (relative to the class path, not the current directory).

And David meant File.separator, not pathSeparator.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Class.getResource doesn't take an absolute file path the way you are trying to do. It takes a relative path to the current class and uses / for path separator; starting with / makes it an absolute path (relative to the class path, not the current directory).

And David meant File.separator, not pathSeparator.



So how may Class.getResource help me with reading the relative path to a string? Any code-snippet, because I couldn't figure it out myself.



Output
\
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:And David meant File.separator, not pathSeparator.


Thanks, serves me right for reading a separate section of the API at the same time.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Camilleri wrote:

Rob Spoor wrote:Class.getResource doesn't take an absolute file path the way you are trying to do. It takes a relative path to the current class and uses / for path separator; starting with / makes it an absolute path (relative to the class path, not the current directory).

And David meant File.separator, not pathSeparator.



So how may Class.getResource help me with reading the relative path to a string? Any code-snippet, because I couldn't figure it out myself.


Assuming the images folder is at the root of your class path:
reply
    Bookmark Topic Watch Topic
  • New Topic