2

100% of the Java work I do is on Android. When constructing file paths, I always make sure to use File.pathSeparator File.separatorChar instead of "/".

What determines the value of File.pathSeperator? Are there any Android devices where File.pathSeparator File.separatorChar will not be "/" ?

Error 454
  • 7,255
  • 2
  • 33
  • 48

2 Answers2

1

According to the source code of File.java, pathSeparator is pulled from the char System.getProperty("path.separator", ":").charAt(0). separator, on the other hand, comes from System.getProperty("file.separator", "/").charAt(0).

Because Android is Linux-based, it should always be /. However, I have seen glitches where : has appeared instead (Motorola Droid; see bug report), by using the default value above.

When I code, I always use /... to be honest, I don't think it's necessary to reference the File class (this would mostly be for non-Android work). It's just best to stick to /, I think.

Additionally, according to this answer (pertains to Java, not just Android), / can safely be used on all Java platforms, and it will figure out what it needs to use internally.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
1

I think you mean File.separator and File.separatorChar right? If you look in the docs it states:

public static final char separatorChar

The system-dependent character used to separate components in filenames ('/'). Use of this (rather than hard-coding '/') helps portability to other operating systems.

This field is initialized from the system property "file.separator". Later changes to that property will have no effect on this field or this class.

It is system-dependent. Technically it could be possible for someone to implement Android on a non-Linux platform (see BlueStacks) where the path separator is something other than '/'. In practice, I'd imagine you can get away with using '/' mostly without issue but it's a good habit to use these fields instead.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • While I do agree with the portability point, in Java, Windows recognizes `/` as well. ([Source](http://www.java-samples.com/showtutorial.php?tutorialid=382)) While I admit that you may not implement Android on Windows (eww!), it's likely most systems will support `/` regardless. – Cat Aug 08 '12 at 20:16