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

Started by
8 comments, last by Aldacron 18 years, 10 months ago
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?
Advertisement
I think / works on both Windows and Linux (at least in C++).
WanMaster is right--forward slashes / will work on both platforms.
Orin Tresnjak | Graphics ProgrammerBethesda Game StudiosStandard Disclaimer: My posts represent my opinions and not those of Bethesda/Zenimax, etc.
The property you are looking for is "file.separator" to be used as

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

See for reference.
Fil (il genio)
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...
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.
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" );
In C/C++/Java, "/" works on all platforms including MacOS.
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
a.k.a javabeats at yahoo.ca
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'.

This topic is closed to new replies.

Advertisement