Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Escape sequences

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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)
 
Stephan van Hulst
Bartender
Pie
Posts: 5387
52
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
;
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic