Java File separator, separatorChar, pathSeparator, pathSeparatorChar

Filed Under: Java class contains four static separator variables. Here we will learn about them and when to use it.

  1. File.separator: Platform dependent default name-separator character as String. For windows, it’s ‘\’ and for unix it’s ‘/’.
  2. File.separatorChar: Same as separator but it’s char.
  3. File.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. File.pathSeparatorChar: Same as pathSeparator but it’s char.

Java File separator

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

java file separator, java path separator, separatorChar

Here is the java program to print these separator variables.

package com.journaldev.files;


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");

That’s all for file separators, we will learn more about java io in future posts.


  1. Amit says:

    I really impressed..

  2. Marc says:

    Thank you for this. Very useful and well explained.

  3. iZpDh1EmjH3z says:

    427446 685862I discovered your weblog web site on google and check several of your early posts. Continue to maintain up the really excellent operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading a lot more from you later on!

  4. separator tłuszczów says:

    I relish, result in I discovered just what I used to be taking a look for.
    You’ve ended my four day long hunt! God Bless you man. Have a great day. Bye

    1. JD says:

      Really good Explanation…

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors