How do I get file separator symbol?

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use “” (back slash) while Linux use “/” (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.

package org.kodejava.example.lang;

public class FileSeparatorExample {
    public static void main(String[] args) {
        //
        // file.separator syste property return the correct file separator for
        // each different platform (Windows = ), (Linux = /)
        //
        String dataFolder = System.getProperty("user.dir") +
                System.getProperty("file.separator") + "data";
        
        System.out.println("Data Folder = " + dataFolder);
    }
}

Wayan Saryada

A programmer, a slow runner, a recreational open water diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply