615

In the File class there are two strings, separator and pathSeparator.

What's the difference? When should I use one over the other?

David Medinets
  • 5,160
  • 3
  • 29
  • 42
icnhzabot
  • 9,801
  • 5
  • 21
  • 9
  • 7
    The naming is a bit confusing, the fast that something like this is needed is plain terrible (cf. Perl). Look at the examples for [pathSeparatorChar](http://download.oracle.com/javase/1,5.0/docs/api/java/io/File.html#pathSeparatorChar) and [separatorChar](http://download.oracle.com/javase/1,5.0/docs/api/java/io/File.html#separatorChar). Or use the simple mnemonics: the pathSeparator separates paths. – maaartinus May 12 '11 at 00:25
  • 7
    Taking a minute to print both of them to screen would have answered your question... – Jean-François Corbett Jun 19 '14 at 13:31
  • 17
    While I'd generally agree, simply printing them on his system isn't going to show the variants for other operating systems. – arkon Apr 01 '15 at 02:11

3 Answers3

784

If you mean File.separator and File.pathSeparator then:

  • File.pathSeparator is used to separate individual file paths in a list of file paths. Consider on windows, the PATH environment variable. You use a ; to separate the file paths so on Windows File.pathSeparator would be ;.

  • File.separator is either / or \ that is used to split up the path to a specific file. For example on Windows it is \ or C:\Documents\Test

Kaadzia
  • 1,393
  • 1
  • 14
  • 34
user489041
  • 27,916
  • 55
  • 135
  • 204
  • 12
    Seems like `File.separator` should be `File.fileSeparator` regarding to `File.pathSeparator` – Eddy Dec 21 '16 at 06:40
  • 4
    @Eddy I see your point, but it might be redundant since the class name is `File`. I think the file part is implied. But who knows why they did a lot of what they did with Java. – user489041 Dec 21 '16 at 17:34
146

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");
mkobit
  • 43,979
  • 12
  • 156
  • 150
foxt7ot
  • 2,465
  • 1
  • 19
  • 30
  • 2
    Note that in Java the backslash character actually is \\, because a single backslash is the escape character for other special character shorthands, so the backslash itself is used to escape itself. The `String` and `char` returned by the above-mentioned methods do return the properly formatted backslash (if on Windows). – Erik Apr 16 '17 at 23:06
  • 1
    new File("tmp/abc.txt"); this is corectly for windows and linux but this is not corectly for unix new File("tmp\\abc.txt"); this is problem only unix – DEV-Jacol Apr 30 '20 at 07:05
  • @DEV-Jacol it is not quite clear in your comment, but are you conflating Linux and Unix? Because they are not the same :) – DownloadPizza Aug 23 '22 at 13:29
122

You use separator when you are building a file path. So in unix the separator is /. So if you wanted to build the unix path /var/temp you would do it like this:

String path = File.separator + "var"+ File.separator + "temp"

You use the pathSeparator when you are dealing with a list of files like in a classpath. For example, if your app took a list of jars as argument the standard way to format that list on unix is: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar

So given a list of files you would do something like this:

String listOfFiles = ...
String[] filePaths = listOfFiles.split(File.pathSeparator);
Adam
  • 43,763
  • 16
  • 104
  • 144
Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53
  • 6
    If you are building a *nix path like `/var/temp` then it's useless to use `File.separator` since you already have platform-dependent code. Might as well hardcode the path. – isapir Nov 15 '16 at 18:25
  • @isapir Yes I use File.separator less and less all the time. If you have a path generated and stored inside a file (or a database) that goes to another system, using File.separator on Win and then moving it to Linux will just cause a problem. Safer to just use "/". I was wondering if someone knows a reason to do otherwise but I don't see one here. – Manius Sep 17 '22 at 23:22