How do I get file separator symbol?

Category: java.lang, viewed: 15065 time(s).

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);
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats