What is path separator in Java?

To display the path separator, use the Properties class and import the following package −

import java.util.Properties;

Use the getProperties() method first and create an object −

Properties p = System.getProperties();

Now, set the key for path separator −

p.getProperty("path.separator")

The following is an example −

Example

 Live Demo

import java.util.Properties;
public class Demo {
   public static void main(String[] args) {
      Properties p = System.getProperties();
      System.out.println("Separator is "+p.getProperty("path.separator"));
   }
}

Output

Separator is :

In this example, path.separator is used to separate the path and file by semicolon (;). When it is run, Ant checks for the path separator and directory separator characters provided by the underlying JVM and uses those values. It successfully interprets either the ";" or the ":" inside the build file. For example, when run on Unix machine, ant interprets the path dir;dir\\subdir correctly as the dir;dir\\subdir. Separator must be used consistently with in the same value type; the string dir;dir\\subdir, combining a windows path separator (;) and a Unix directory separator (/) is not a good form. 

 

 

 

<project name="PathSeperator" default="echo" basedir=".">

  <target name="echo">
  <echo message="File: ${basedir}${path.separator}build.xml"/>
  <echo message="Path: ${basedir}${path.separator}build.xml${path.separator}
   ${basedir}${path.separator}build.properties"/>
  </target>

</project>

The following output will be displayed if you execute ant command on the command prompt with appropriate path. 

What is path separator in Java?


Download Source Code

What is path separator in Java?
    
What is path separator in Java?
 
What is path separator in Java?

The default name-separator character is defined by the system property file.separator, and is made available in the public static fields separator and separatorChar of this class.

The usual way to construct a path is either

String fs = File.separator;

or

String fs = System.getProperty("file.separator");

and then construct a path like

String myPropsPath = "core" + fs + "main.properties";

Now, why did I say that you should not use file separator when loading resources? The reason is that a file and a resource is not the same thing. In most cases a resource will be a file on the disk and even is you use file separator you would not have any problems loading it (Java is smart).
However, there is a corner case, which will hit you if you are not prepared. Consider the following example:

URL url = this.getClassLoader().getResource(myPropsPath);

This is how you get a URL to a resource. The code works fine most of the time. The resource being looked up is found on the classpath. The only problem is when this resource resides inside an archive (ZIP, JAR, WAR, etc.) and the code is executed on Windows platform.
I discovered it when I was debugging some code that was constructing a path using the file separator character and the application was deployed as a WAR module. This WAR was not exploded by the application server and the application server was running on Microsoft Windows.
Why it did not work? Simply, the path was incorrect. It took me a while to figure out why. I checked the value in myPropsPath. It looked fine. It was using the back-slashes as it should on Windows. The file was in the right location. And then I noticed a difference. There was another file beside it. The application loaded it just fine. The difference was that its path was not constructed this way, it was hard-coded in the XML file and it used forward-slashes as the file separators.
Then I looked at the JavaDoc on ClassLoader.getResource(String) method and as Stuart Pearce would say: “I saw the carrot at the end of the tunnel.” ClassLoader requires a name of the resource as a /-separated path name. So, Windows back-slashes won’t work.
Just to show you some outputs, consider the following class

01: public class Loader {
02:
03:   public static void main(String[] args) {
04:     String separator = args[0];
05:     System.out.println("Separator is '" + separator + "'");
06:     String path = "com" + separator + "atlassian" + separator + "slash"
07:                 + separator + "SomeClass.class";
08:     System.out.println("Path is '" + path + "'");
10:     URL url = Loader.class.getClassLoader().getResource(path);
11:     System.out.println("URL is '" + url + "'");
12:     if (url != null) {
13:       try {
14:         File file = new File(url.toURI());
15:         System.out.println("File exists: " + file.exists());
16:       } catch (URISyntaxException e) {
17:         System.out.println("Error in URI syntax");
18:       } catch (IllegalArgumentException e) {
19:         System.out.println(e.getMessage());
20:       }
21:     }
22:   }
23: }

When running this little program, we manually set the file separator character as the first parameter passed into the main method on line 4. Then we construct a path on line 6. We try to get the URL to the resource on line 10. If the URL is retrieved and not null, we try to create a File object using the URI from URL (line 14).
There are the following 4 combinations that we will test:
1. and resource in JAR file;
2./ and resource in JAR file;
3. and resource as a file on the disk;
4./ and resource as a file on the disk.
The first case gives the following output:

Separator is ''
Path is      'comatlassianslashSomeClass.class'
URL is       'null'

As you can see constructing a URL with back-slashes is not valid.
The second test scenario produces:

Separator is '/'
Path is      'com/atlassian/slash/SomeClass.class'
URL is       'jar:file:/C:/SomeJAR/release/somejar.jar!/com/atlassian/slash/SomeClass.class'
URI is not hierarchical

The last message indicates that URL is correct, however it is not a file and cannot be accessed this way.
The third and fourth run prints out:

Separator is ''
Path is      'comatlassianslashSomeClass.class'
URL is       'file:/C:/SomeJAR/bin/com%5catlassian%5cslash%5cSomeClass.class'
File exists: true

Separator is '/'
Path is      'com/atlassian/slash/SomeClass.class'
URL is       'file:/C:/SomeJAR/bin/com/atlassian/slash/SomeClass.class'
File exists: true

As you can see Java is clever and when you are accessing files, it can handle both forward and back-slashes just fine.
The moral of this story is, use system-specific file separator only when working with files and when displaying a path to the user. For all other cases use forward slash.

What is separator char in Java?

separator: Platform dependent default name-separator character as String. For windows, it's '\' and for unix it's '/'. File. separatorChar: Same as separator but it's char.

What is the symbol for path delimiter?

The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter.

Which of following is the file separator?

Which of following is the file separator? The file. separator is the system property containing the character (or characters) that delimits file and directory names. This character is usually / or \ .

What is path separator for Mac?

The path. separator is the character used to separate path entries on a single line (such as multiple entries in the system's classpath). Generally, either you will have a base directory and need to construct paths relative to this directory, or you will work with user-specified files and use standard file dialogs.