Last Updated:

File path separator | Java With Examples

A little theory on each file path separator

  1. The Variable File separator is a platform-specific character that is used to separate directories in the path to a file. For example, for Windows it is , and for UNIX it is .'\''/'
  2. The variable separatorChar is the same in the separator, but is the character .char
  3. PathSeparator variable: A platform-dependent variable that is used in PATH or CLASSPATH. It is a separator on Unix and Windows systems.':'';'
  4. The variable pathSeparatorChar: The same as pathSeparator, but is a .char

File path separator in practice

Here is a Java program that prints these delimiters into the console:

The result of running the program on Unix-systems:

The result of the program execution in Windows-systems:

In order for our program to be platform-independent, we must always use these delimiters to create the path to the file.

Example of proper use of File.separator

 

That's all you need to know about file separators in Java. Stay tuned to the Java IO section.