Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



Java and file paths


5 replies to this topic - - - - -

#1 timsweb

    Resident Elite

  • 1,554 posts
  • Joined: 19-February 04
  • Location: UK

Posted 17 July 2012 - 09:27

Hi all,

Have a quick question regarding Java. I am making a application that works alot with file paths and will be used on different platforms and I am having problems with back slashes and forward slashes. I want to use forward slashes as they seem more universal and cross platform but when I use a instance of File in Java to get paths it returns them using backward slashes, is their a way to change this?

Thanks, Tim


#2 drcrawfo

    Neowinian

  • 3 posts
  • Joined: 17-July 12

Posted 17 July 2012 - 15:39

Ideally you should be using File.separator instead of a forward slash. Java will likely convert a forward slash to the proper file separator of the system, but I don't believe this is absolutely guaranteed for all platforms. As far as getting a path from a File, this is again system dependent. You will always get a path that uses the systems file separator.

#3 OP timsweb

    Resident Elite

  • 1,554 posts
  • Joined: 19-February 04
  • Location: UK

Posted 22 July 2012 - 13:01

Okay thanks drcrawfo, solved it by writting a quick method which changes any backward slashes to forward so will give the same result whatever the OS,

Thanks! Tim

#4 tiagosilva29

    If you're in trouble PM me

  • 11,890 posts
  • Joined: 08-May 04

Posted 22 July 2012 - 14:20

Java 7 added lots of magic in the java.nio.file package.


#5 thatguyandrew1992

    Neowinian Senior

  • 2,078 posts
  • Joined: 22-January 09

Posted 22 July 2012 - 21:35

View Posttimsweb, on 22 July 2012 - 13:01, said:

Okay thanks drcrawfo, solved it by writting a quick method which changes any backward slashes to forward so will give the same result whatever the OS,

Thanks! Tim
Someone correct me if I'm wrong, but isn't doin what Tim is doing bringing up the possibility of errors? Aren't there more appropriate ways to handling this, like using file seperator like drcrawfo suggested?

#6 +GreenMartian

    Resident Elite

  • 1,687 posts
  • Joined: 28-August 04
  • Location: adelaide, au

Posted 22 July 2012 - 22:08

View Posttimsweb, on 22 July 2012 - 13:01, said:

... writting a quick method which changes any backward slashes to forward so will give the same result whatever the OS,
Whoa. Careful there. *nix does use backslashes for escaping characters, and changing them to forward slashes would completely change their meaning.

As everyone else has been urging you, do use the File.separator constant.