Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Different operating systems use different characters as file and path separators. When our application has to run on multiple platforms, we need to handle these correctly.

Java helps us to pick an appropriate separator and provides functions to help us create paths that work on the host’s operating system.

In this short tutorial, we’ll understand how to write code to use the correct file and path separators.

2. File Separator

The file separator is the character used to separate the directory names that make up the path to a specific location.

2.1. Get the File Separator

There are several ways to get the file separator in Java.

We can get the separator as a String using File.separator:

String fileSeparator = File.separator;

We can also get this separator as a char with File.separatorChar:

char fileSeparatorChar = File.separatorChar;

Since Java 7, we’ve also been able to use FileSystems:

String fileSeparator = FileSystems.getDefault().getSeparator();

The output will depend on the host operating system. The file separator is \ on Windows and / on macOS and Unix-based operating systems.

2.2. Construct a File Path

Java provides a couple of methods for constructing a file path from its list of directories.

Here, we’re using the Paths class:

Path path = Paths.get("dir1", "dir2");

Let’s test it on Microsoft Windows:

assertEquals("dir1\\dir2", path.toString());

Similarly, we can test it on Linux or Mac:

assertEquals("dir1/dir2", path.toString());

We can also use the File Class:

File file = new File("file1", "file2");

Let’s test it on Microsoft Windows:

assertEquals("file1\\file2", file.toString());

Similarly, we can test it on Linux or Mac:

assertEquals("file1/file2", file.toString());

As we see, we can just provide path strings to construct a file path — we don’t need to include a file separator explicitly.

3. Path Separator

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

3.1. Get the Path Separator

We can get the path separator as a String using File.pathSeparator:

String pathSeparator = File.pathSeparator;

We can also get the path separator as a char:

char pathSeparatorChar = File.pathSeparatorChar;

Both examples return the path separator. It is a semicolon (;) on Windows and colon (:) on Mac and Unix-based operating systems.

3.2. Construct a File Path

We can construct a file path as a String using the separator character as a delimiter.

Let’s try the String.join method:

String[] pathNames = { "path1", "path2", "path3" };
String path = String.join(File.pathSeparator, pathNames);

Here we test our code on Windows:

assertEquals("path1;path2;path3", path);

And file path will look different on Linux or Mac:

assertEquals("path1:path2:path3", path);

Similarly, we can use the StringJoiner class:

public static StringJoiner buildPathUsingStringJoiner(String path1, String path2) {
    StringJoiner joiner = new StringJoiner(File.pathSeparator);
    joiner.add(path1);
    joiner.add(path2);
    return joiner;
}

Let’s test our code on Microsoft Windows:

assertEquals("path1;path2", buildPathUsingStringJoiner("path1", "path2"));

And it will behave differently on Mac or Unix:

assertEquals("path1:path2", buildPathUsingStringJoiner("path1", "path2"));

4. Conclusion

In this short article, we learned how to construct paths using system-specific file separators so our code can work on multiple operating systems.

We saw how to use the built-in classes Path and File to construct file paths, and we saw how to get the necessary separator to use with String concatenation utilities.

As always, the example code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.