File.separator vs File.pathSeparator



  • File.separator == > separator: Platform dependent default name-separator character as String. For           windows, it’s ‘\’ and for unix it’s ‘/’.
  • File.separatorChar ==>separatorChar: Same as separator but it’s char.
  • File.pathSeparator ==> 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.
  • File.pathSeparatorChar ==> pathSeparatorChar: Same as pathSeparator but it’s char.
  • This is the environment independent property.This will be usefull for file upload and downloading etc.



  1. package commonsUtils;
  2. import java.io.File;

  3. public class FileSep{
  4. public static void main(String[] args){
  5.                   String filepath = File.separator + "sample" + File.separatorChar + "seperator";
  6.                   String pathSep = File.pathSeparator + "sample" + File.pathSeparatorChar + "seperator";                                              

  7.                   System.out.println("The path of the file is : " + filepath);
  8.                   System.out.println("The path of the file is  :  " + pathSep);

  9.  System.out.println("File.separator = "+File.separator)
  10.  System.out.println("File.separatorChar = "+File.separatorChar);
  11.  System.out.println("File.pathSeparator = "+File.pathSeparator);
  12.  System.out.println("File.pathSeparatorChar = "+File.pathSeparatorChar);
  13. }
  14. }


 Output :

 For Windows
 -------------
 The path of the file is  :  \sample\seperator
 The path of the file is  :  ;sample;seperator
 File.separator = \
 File.separatorChar = \
 File.pathSeparator = ;
 File.pathSeparatorChar = ;

 For Unix
 ----------
 The path of the file is  :  /sample/seperator
 The path of the file is  :  :sample:seperator
 File.separator = /
 File.separatorChar = /
 File.pathSeparator = :
 File.pathSeparatorChar = :

No comments:

Post a Comment