LRFLEW
LRFLEW

Reputation: 1291

how do I make path names compatible with different OSs?

I used to know it, but I know there is a way in java to make sure the path works in all OSs (Windows uses a different "/" system than Unix based systems). What do I have to do to create a file path name that works with all systems?

EDIT: I wasn't sure if this is what I meant, but I think the file path will end up being relative.

Also, what's the difference between File.separator and File.pathSeparator ?

Upvotes: 0

Views: 1142

Answers (4)

LudoMC
LudoMC

Reputation: 760

You'll have to use File.separator or File.separatorCharfor this purpose.
Check the File class javadoc for more info on this.

edit: Difference between separator and pathSeparator. The first separates the folder in a file's path like the / in /usr/bin. The second one is a separator in a path environment variable, like the ; in PATH=C:/windows/bin;C:/anotherfolder

Upvotes: 6

John Giotta
John Giotta

Reputation: 16934

I've used this before, except I'm not sure of its popularity.

System.getProperty("file.separator");

Upvotes: 1

asgs
asgs

Reputation: 3984

File has a separator

Upvotes: 2

Matt Ball
Matt Ball

Reputation: 359786

You can just use / (forward slash) as the path separator. That will work cross-platform.

Upvotes: 4

Related Questions