Tutorial

Java File separator, separatorChar, pathSeparator, pathSeparatorChar

Published on August 4, 2022
author

Pankaj

Java File separator, separatorChar, pathSeparator, pathSeparatorChar

java.io.File 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;

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

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 14, 2014

I really impressed…

- Amit

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 11, 2014

    Thank you for this. Very useful and well explained.

    - Marc

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 19, 2013

      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!

      - iZpDh1EmjH3z

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 11, 2013

        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

        - separator tłuszczów

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Become a contributor for community

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          DigitalOcean Documentation

          Full documentation for every DigitalOcean product.

          Resources for startups and SMBs

          The Wave has everything you need to know about building a business, from raising funding to marketing your product.

          Get our newsletter

          Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

          New accounts only. By submitting your email you agree to our Privacy Policy

          The developer cloud

          Scale up as you grow — whether you're running one virtual machine or ten thousand.

          Get started for free

          Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

          *This promotional offer applies to new accounts only.