If you're here, you're probably writing some Java code that needs to construct a whole directory path on the fly. And you're wondering how to do it.

Specifically, you'd like to know how to insert the proper separators between subdirectories in the path.

Well then you've come to the right place because I'll show you how to do that right here.

Backwards or Forwards?

Alas, not all operating systems are created equal. Some of them use the backslash to separate subdirectories (I'm looking at you, Bill Gates).

With MS-DOS, for example, a standard file path looks like this:

\etc\careydevelopment\ecosystem.properties

Note that the slashes are backwards. Not forwards.

It's a different story on the various flavors of Unix. That same path would look like this:

/etc/careydevelopment/ecosystem.properties

And now as a Java developer you're tasked with a requirement that tells you to build a directory (like the one above) such that it runs on any operating system.

That "any operating system" part is the kicker. After all, Java is "write once, run anywhere" so whatever you do should work on Unix or Winblows.

Fortunately, you have options.

Do Nothing

Your first option is to do nothing and always use the forward slash.

Yep. That will work. On any platform.

It's part of the "write once, run anywhere" schtick. 

Ah, but that might not work for your requirement. You might need to format a String for output and that String has to use the path separator required by the underlying operating system.

Relax. You still have options.

System Normal, AFU

Probably the most popular option is to use System.

Specifically, use:

System.getProperty("file.separator");

That's going to give you a String that displays a single character. That character will be the file separator on your operating system.

Run this code:

System.err.println(System.getProperty("file.separator"));

If you're on Unix, that will print out a forward slash. If you're on that thing Bill Gates created, you'll get a backslash.

So one thing you could do when constructing a directory string is to set a constant equal to the file separator and just use that constant to build your paths.

private static final String SEPARATOR = System.getProperty("file.separator");

public static void main(String[] args) {      

    String directory =  SEPARATOR + "etc" 
                            + SEPARATOR + "careydevelopment" 
                            + SEPARATOR + "ecosystem.properties";
    
    System.err.println(directory);

}

Run that one and it's going to print out this if you're looking through Windows:

\etc\careydevelopment\ecosystem.properties

On Unix, the slashes will face the other direction.

Java NIO

Another option: go with Java NIO.

Specifically, try this code:

FileSystems.getDefault().getSeparator();

Yeah. You know what that's going to give you by now.

Here it is in action:

private static final String SEPARATOR = FileSystems.getDefault().getSeparator();

public static void main(String[] args) {      

    String directory =  SEPARATOR + "etc" 
                            + SEPARATOR + "careydevelopment" 
                            + SEPARATOR + "ecosystem.properties";
    
    System.err.println(directory);

}

Note that the only difference between that code block and the code block in the last section is the very first line.

But it will give you the same output. Go ahead and run it.

Why would you choose this option over the previous one? Honestly, I can't think of a reason.

Java IO

If you still like dabble with a development kit from the Dark Ages of Java programming, you can get the path separator from the File class.

Just use File.separator.

private static final String SEPARATOR = File.separator;

public static void main(String[] args) {      
 
    String directory =  SEPARATOR + "etc" 
                            + SEPARATOR + "careydevelopment" 
                            + SEPARATOR + "ecosystem.properties";
    
    System.err.println(directory);

}

Run that and guess what? You'll get the same output that you saw on with previous two code blocks.

And once again: note that the only difference is in the first line of code.

Wrapping It Up

So there you have it: a few different ways you can grab that file separator.

Or, if you prefer, do nothing.

Whichever option you choose, get busy coding. Run some tests and make sure it works.

Have fun!

Photo by Alex Green from Pexels