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.

The class contains two static variables which can make file path separation in your java code platform-independent.

  1. File.separator : This is the String value that your os used to separate file paths. So to make your java code correctly, you should use the below code when creating 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 the 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.

The path separator is used to separate multiple values in the system environment variable( such as PATH, CLASSPATH, etc )’s value.

If you are using windows. Open a dos command line window, input the command set, then you can see the system environment variable Path and its value. You can see the multiple path value is separated by ;. For Unix and Linux, path variable value is separated by :.

  1. File.pathSeparator : This is the static variable that returns the 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 The Current Working Directory.

Sometimes 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 tree, then click the Export menu item in the popup menu list.
  2. Choose “Runnable JAR File” in the popup Export window.
  3. Choose “FileSeparatorExample” class in the Launch configurations drop-down list, select a target folder in the Export destinations drop-down input text box.
  4. Now go to the runnable jar saved folder. And run the jar file with the command java -jar Dev2qaExample.jar, you can see the below output.
    c:\WordSpace>java -jar Dev2qaExample.jar
    Current working directory = c:\WorkSpace

4. Get All System Environment Variables And Their Value.

Sometimes when you want to get some value from the system environment variable, you may 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.

The below example will iterate the Properties object and print out each system environment variable name and its 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());

Below is the output when you run the above source code: = OpenJDK Runtime Environment
sun.boot.library.path = /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib
java.vm.version = 25.212-b03
gopherProxySet = false
java.vm.vendor = AdoptOpenJDK
java.vendor.url =
path.separator = : = OpenJDK 64-Bit Server VM
file.encoding.pkg = = US = SUN_STANDARD
sun.os.patch.level = unknown = Java Virtual Machine Specification
user.dir = /Users/songzhao/Documents/WorkSpace/
java.runtime.version = 1.8.0_212-b03
java.awt.graphicsenv = sun.awt.CGraphicsEnvironment
java.endorsed.dirs = /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/endorsed
os.arch = x86_64 = /var/folders/qr/mrbn885s649ckh9tm8rzllrc0000gn/T/
line.separator = 

java.vm.specification.vendor = Oracle Corporation = Mac OS X
sun.jnu.encoding = UTF-8
java.library.path = /Users/songzhao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. = Java Platform API Specification
java.class.version = 52.0 = HotSpot 64-Bit Tiered Compilers
os.version = 10.15.7
user.home = /Users/songzhao
user.timezone = 
java.awt.printerjob = sun.lwawt.macosx.CPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
java.class.path = /Users/songzhao/Documents/WorkSpace/ = songzhao
java.vm.specification.version = 1.8 =
java.home = /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre = 64
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.lwawt.macosx.LWCToolkit = mixed mode
java.version = 1.8.0_212
java.ext.dirs = /Users/songzhao/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
sun.boot.class.path = /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/classes
java.vendor = AdoptOpenJDK
file.separator = /
java.vendor.url.bug = = UnicodeBig
sun.cpu.endian = little
sun.cpu.isalist =

3 thoughts on “Java File Separator And Current Working Directory Examples”

  1. I find I can use both java File.separator and a / character in a path string for java application. For example, the java code File file = new File(“/usr/local/”); and File file = new File(File.separator + “usr” + File.separator + “local” + File.separator + “”); can both execute on windows and Linux without error. So my question is why we need to use File.separator since the / character can be used on both windows and Linux?

  2. I find the java File class has two static variable string, one is separator the other is pathSeparator. Can you tell me the difference between them? When should I use the separator and when should I use the pathSeparator? Thanks.

Leave a Comment

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.