Do I need to escape a separator character in a file path in java

149 Views Asked by At

There are many questions on escape characters but I seem unable to find an answer when using File.separator

I am using a system dependant separator character which I am creating with this code:

public class SeparatorCharactor
{
    public static String createSeparatorCharactor()
    {
        String sep = File.separator;
        return sep;
        //I could use *return File.separator;* but I prefer to do it in 2 steps.
    }
}

My understanding is that this creates a file separator that will work on any operating system.

I am then using the file separator like this :

private String sep = SeparatorCharactor.createSeparatorCharactor();
private String path = "S:"
   + sep + "folder1"
   + sep + "folder2";

My Question is : Do I need to escape the file separator ?

In other words do I need :

  1. "S" + sep + "folder1"

or

  1. "S" + sep + sep + "folder1"
3

There are 3 best solutions below

3
Basil Bourque On BEST ANSWER

Do I need to escape the file separator ?

No, you do not need to escape the separator in a path in Java.

Just use the forward-slash character (/) to indicate a file path separator in a generic sense.

The file-related libraries in Java automatically translate the forward-slash character to the platform-specific character.

The / translates to:

  • / on Unix-oriented platforms such as macOS, Linux, AIX, and BSD.
  • \ on Microsoft Windows.
  • : on legacy Mac OS 9 and earlier.

do I need : "S" + sep + "folder1" or "S" + sep + sep + "folder1"

Neither.

In modern Java, use the convenient classes provided by NIO. See Path.of or Paths.get.

Path path = Path.of( "S", "folder1" , "folder2" );

separator character which I am creating with this code

No, do not create a method for that purpose. Your method adds no value. Calling that method will only confuse anyone reading your code.

If you want to use a constant provided by the JVM, just use it. Just call File.separator directly.


Tip: Be careful in selecting a tutorial or article about file-handling in Java.

The early versions of Java had file-related classes that were awkward and confusing. Many pages were written in trying to explain these. Avoid such outdated material.

Those legacy file-handling classes were supplanted by the modern classes of NIO and NIO.2. Focus on these classes when studying Java.

3
Michael Gantman On

Definitely not. You don't need to escape it. But also in your code "S" + sep + sep + "folder1" you rely that the value of your sep variable is "\" which is not guaranteed. So if you wanted to escape it you would have to do "S" + "\\" + sep + "folder1". But again not only you don't need to do it, but if you do you will get unexpected and incorrect result

5
3rdRockSoftware On

Thank you all for all your answers.

I have now adapted my code to use the Path API like this:

import java.nio.file.FileSystems;
import java.nio.file.Path;

private Path path = FileSystems.getDefault().getPath(
            "Root",
            "folder1",
            "folder2",
            "filename.zzz"
    );

This works perfectly.

The complete path can be accessed by using path.toString() //"C:\Root\folder1\folder2\filename.zzz"

The parent folder = path.getParent() //"C:\Root\folder1\folder2"

The filename = path.getFilename() //"filename.zzz"