Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Escape sequences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Escape sequences " Watch "Escape sequences " New topic
Author

Escape sequences

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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)

Jon
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Escape sequences
 
Similar Threads
images in jar file
Loading from a resource folder returns null
problem with MyClass.class.getResource()
loading image
Loading an Image