When Should I Use File.Separator and When File.Pathseparator

File.separator vs. File.pathSeparator

java.io.File class contains four static separator variables. For better understanding, Let's understand with the help of some code

  1. separator: Platform dependent default name-separator character as String. For windows, it’s ‘\’ and for unix it’s ‘/’
  2. separatorChar: Same as separator but it’s char
  3. pathSeparator: Platform dependent variable for path-separator. For
    example PATH or CLASSPATH variable list of paths separated by ‘:’ in
    Unix systems and ‘;’ in Windows system
  4. pathSeparatorChar: Same as pathSeparator but it’s char

Note that all of these are final variables and system dependent.

Here is the java program to print these separator variables.
FileSeparator.java

import java.io.File;

public class FileSeparator {

public static void main(String[] args) {
System.out.println("File.separator = "+File.separator);
System.out.println("File.separatorChar = "+File.separatorChar);
System.out.println("File.pathSeparator = "+File.pathSeparator);
System.out.println("File.pathSeparatorChar = "+File.pathSeparatorChar);
}

}

Output of above program on Unix system:

File.separator = /
File.separatorChar = /
File.pathSeparator = :
File.pathSeparatorChar = :

Output of the program on Windows system:

File.separator = \
File.separatorChar = \
File.pathSeparator = ;
File.pathSeparatorChar = ;

To make our program platform independent, we should always use these separators to create file path or read any system variables like PATH, CLASSPATH.

Here is the code snippet showing how to use separators correctly.

//no platform independence, good for Unix systems
File fileUnsafe = new File("tmp/abc.txt");
//platform independent and safe to use across Unix and Windows
File fileSafe = new File("tmp"+File.separator+"abc.txt");

Difference between File.separator and slash in paths

With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

You might want to use File.separator in UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to Java.

Update: I have not been able, in five minutes of searching, to find the "you can always use a slash" behavior documented. Now, I'm sure I've seen it documented, but in the absense of finding an official reference (because my memory isn't perfect), I'd stick with using File.separator because you know that will work.

/, // and File.Separator

Forward-slash works sometimes on Windows. The file system and the kernel treat '/' and '\' equivalently in path names. This has always been the case in the Windows NT line (2000, XP, Vista, 7, 8, 10) but was never true for DOS-based Windows.

Application code is not necessarily so accommodating. Code written by someone unaware of this convention may not understand '/'.

Then, too, there's the problem of command-line processing, where '/' may indicate a switch rather than a path.

So, 'Windows' itself understands '/' and '\' to be equivalent in pathnames, but not all code that runs in Windows knows that.

tl;dr - sometimes it works, sometimes it does not.

Gradle file system. Use '/' or File.separator?

I always use / and it works fine.

Even in the Gradle docs, they don't bother so I'd imagine it's reasonably safe.

How to use File.separator in Windows

Since the argument of String.split is a regular expression, you need to quote the separator for it to be treated as a literal:

String[] split = strData.toString().split(Pattern.quote(File.pathSeparator));

Why is File.pathSeparatorChar a semicolon on Windows?

The PATH separator has been a semicolon for a very long time, presumably since the very first release of MS-DOS. (I'm assuming, as per Thorsten's answer, that Java simply defered to the Windows convention, presumably because Java programmers are likely to assume that they can use pathSeparatorChar to parse the value of PATH rather than only to parse file lists produced by Java itself.)

The most obvious options for such a separator (by analogy with English) are the period, the comma, and the semicolon. The period would conflict with the 8.3 file name format. The choice of the semicolon over the comma may well have been arbitrary.

At any rate, semicolons were not legal characters in file names at that time, so there was no reason to prefer the comma. And, of course, since nowadays both commas and semicolons are legal, we wouldn't be any better off if they had. :-)



Related Topics



Leave a reply



Submit