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 java.io.File 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();
	//fse.getOsFileSeparator();
		
	//fse.getOsPathSeparator();
		
	fse.getCurrWorkingDir();
        
}

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.
    export-FileSeparatorExample-as-runnable-jar-file
  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();
	while(it.hasNext())
	{
		Object keyObj = it.next();
		
		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:

java.runtime.name = 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 = http://java.oracle.com/
path.separator = :
java.vm.name = OpenJDK 64-Bit Server VM
file.encoding.pkg = sun.io
user.country = US
sun.java.launcher = SUN_STANDARD
sun.os.patch.level = unknown
java.vm.specification.name = Java Virtual Machine Specification
user.dir = /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject
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
java.io.tmpdir = /var/folders/qr/mrbn885s649ckh9tm8rzllrc0000gn/T/
line.separator = 

java.vm.specification.vendor = Oracle Corporation
os.name = 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.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = 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/dev2qa.com-example-code/JavaCoreExampleProject/target/classes:/Users/songzhao/.p2/pool/plugins/org.junit_4.12.0.v201504281640/junit.jar:/Users/songzhao/.p2/pool/plugins/org.hamcrest.core_1.3.0.v20180420-1519.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-integration-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-ooxml-lite-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-excelant-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-examples-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-ooxml-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-ooxml-full-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-scratchpad-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/poi-5.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/lib/commons-math3-3.6.1.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/lib/SparseBitSet-1.2.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/lib/commons-collections4-4.4.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/lib/commons-codec-1.15.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/ooxml-lib/commons-compress-1.20.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/ooxml-lib/xmlbeans-4.0.0.jar:/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/JavaCoreExampleProject/used-library/poi-5.0.0/ooxml-lib/curvesapi-1.06.jar:/Users/songzhao/.m2/repository/org/apache/poi/poi/5.0.0/poi-5.0.0.jar:/Users/songzhao/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/songzhao/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.30/jcl-over-slf4j-1.7.30.jar:/Users/songzhao/.m2/repository/commons-codec/commons-codec/1.15/commons-codec-1.15.jar:/Users/songzhao/.m2/repository/org/apache/commons/commons-collections4/4.4/commons-collections4-4.4.jar:/Users/songzhao/.m2/repository/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar:/Users/songzhao/.m2/repository/com/zaxxer/SparseBitSet/1.2/SparseBitSet-1.2.jar
user.name = songzhao
java.vm.specification.version = 1.8
sun.java.command = com.dev2qa.java.basic.file.FileSeparatorExample
java.home = /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre
sun.arch.data.model = 64
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.lwawt.macosx.LWCToolkit
java.vm.info = 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 = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = 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/a.java”); and File file = new File(File.separator + “usr” + File.separator + “local” + File.separator + “a.java”); 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.