Java File Separator And Current Working Directory Examples

If you want to make your java code platform independent, especially for java file operation, you should know java file separators. There are four separators by default, they are : File.separatorFile.separatorCharFile.pathSeparator and File.pathSeparatorChar. We will introduce them one by one.

1. Java File Separator class contains two static variable which will be used to make file path separation java code platform independent.

  1. File.separator : This is the String value that your os used to separate file path. So to make your java code correctly, you should use below code when create a path String filePath = "test" + File.separator + "dev2qa.txt"; not use String filePath = "test/dev2qa.txt";.
  2. You can also use System.getProperty("file.separator") to get current os file separator.
  3. File.separatorChar : Return Char type instead of String type.
  4. Example Code.
    	public String getOsFileSeparator()
    		String ret = "";
    		ret = File.separator;
    		ret = System.getProperty("file.separator");
    		ret = String.valueOf(File.separatorChar);
    		System.out.println("Current OS file separator = " + ret);
    		String filePath = "test" + ret + "dev2qa.txt";
    		System.out.println("filePath = " + filePath);
    		return ret;

2.Java Path Separator

Path separator is used to separate multiple value in system environment variable’s value such as PATH, CLASSPATH etc.

If you are using windows. Open a dos command line window. Input command set like below, then you can see the system environment variable Path and it’s value. You can see the multiple Path value is separated by ;. For Unix and Linux, Path variable value is separated by :.

dos set command to list path system environment variable

  1. File.pathSeparator : This is the static variable which return Path separator.
  2. System.getProperty("path.separator") can also be used to get it.
  3. File.pathSeparatorChar : Return Char type instead of String type.
  4. Example code.
    	public String getOsPathSeparator()
    		String ret = "";
    		ret = File.pathSeparator;
    		ret = System.getProperty("path.separator");
    		ret = String.valueOf(File.pathSeparatorChar);
    		System.out.println("Current OS path separator = " + ret);
    		return ret;

3. Get Current Working Directory.

Some times we need to know which directory this java application is running at. We can use System.getProperty("user.dir"). Below is the code example.

	public String getCurrWorkingDir()
		String ret = "";
		ret = System.getProperty("user.dir");
		System.out.println("Current working directory = " + ret);
		return ret;

	public static void main(String[] args) {

		FileSeparatorExample fse = new FileSeparatorExample();

Now export the java class to a runnable jar as below.

  1. Right click the java project in Eclipse left project list tree as below. Click “Export” menu item.
    java hello world export executable jar file
  2. Choose “Runnable JAR File” in popup window.
    choose Runnable JAR files item
  3. Choose “FileSeparatorExample” class, set export destinations as below picture.
    export FileSeparatorExample as runnable jar file
  4. Now go to the runnable jar saved folder. And run it, you can see below output.
    get current working directory in java output
READ :   Java Delete Directory / File Examples

4. Get All System Environment Variables And Their Value.

Some times when you want to get some value from system environment variable, you may can not remember the variable name.

But System.getProperties() can be used to get all system environment variable and their value in a java.util.Properties object.

Below example will iterate the Properties object and print out each system environment variable and it’s value.

public void getAllSystemEnvVariable()
		Properties allProps = System.getProperties();
		/* Get the keys object. */
		Set keySet = allProps.keySet();
		/* Iterate the key and get the related value. */
		Iterator it = keySet.iterator();
			Object keyObj =;
			String key = (String)keyObj;
			Object valObj = allProps.get(key);
			System.out.println(key + " = " + valObj.toString());

Output of above code:

all system environment variable name and value

5. Example java code download.

  1. Download “” – Downloaded 54 times – 752 B

(Visited 2,037 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.