• 0

Java and file paths


Question

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

Link to comment
https://www.neowin.net/forum/topic/1091775-java-and-file-paths/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

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.

  • 0

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?

  • 0

... 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.

This topic is now closed to further replies.