• Advertisement
Sign in to follow this  

[java] How to know if I should use / or \ ?

This topic is 4649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an XML file from which I read the image file names. Let's say I read the following string "data\images\tank.png" and then I try to load the image with ImageIO. Under Windows, this will work, but under Linux it won't, probably because of the \ slash needs to be replaced with a / So is there a way to make Java load a file on every system, no matter the slashes? Or perhaps I can find out which slash does the current system use, and if its / then I can pass through the String and replace the slashes? How can I find out which slashes are used by the system, then?

Share this post


Link to post
Share on other sites
Advertisement
I think / works on both Windows and Linux (at least in C++).

Share this post


Link to post
Share on other sites
WanMaster is right--forward slashes / will work on both platforms.

Share this post


Link to post
Share on other sites
The property you are looking for is "file.separator" to be used as

String FileSeparator=System.getProperty("file.separator");

See for reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fil
The property you are looking for is "file.separator" to be used as

String FileSeparator=System.getProperty("file.separator");

See for reference.


Bah, I was trying with path.separator and kept receiving :
Thanks, that's what I needed. I could just use / but then I'm not sure if it will work on all systems, like Mac for example...

Share this post


Link to post
Share on other sites
If you use file: URIs as names, you should have no problems no matter the platform since the URI format is standardized. Just feed an URI object to java.io.File and use the created object to open your File(In|Out)putStream.

Share this post


Link to post
Share on other sites
You might even consider this: working with java.io.File and friends will fail as soon as you start packaging your program in JAR-files.

The Class-class contains a method getResourceAsStream(String name) which will give you a InputStream to the file (or null if it could not be found). The name-field is a relative path that always uses forward slashes. Example:

InputStream in = ImageResourceLoader.class.getResourceAsStream( "data/images/tank.png" );

Share this post


Link to post
Share on other sites
In C/C++/Java, "/" works on all platforms including MacOS.

Share this post


Link to post
Share on other sites
Everything that was necessary was said, but DaBono's post made me remember my pain when working with JARs and servlets. At that context, you must get the real path to a determined resource.

I never got to understand the use of the 'Class' class to load resources, so I would love to see a small example =D. Also, if you could be so kind as to explain the difference between this approach and the FileEntry class for JARs... I would be very thankfull!

Son Of Cain

Share this post


Link to post
Share on other sites
Quote:
Original post by DaBono
You might even consider this: working with java.io.File and friends will fail as soon as you start packaging your program in JAR-files.


I think what you mean to say here is 'as soon as you start packaging your resources in Jar files'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement