Saturday, 29 April 2023

File separator, separatorChar, pathSeparator, pathSeparatorChar in Java

In this post, I am going to explain about 4 properties in ‘java.io.File’ package.

 

a.   File.separatorChar : The system-dependent default name-separator character.  This field is initialized to contain the first character of the value of the system property 'file.separator'.  On UNIX systems the value of this field is '/' on Microsoft Windows systems it is '\\'.

b.   File.separator: Same as separatorChar, but it is a String.

c.    File.pathSeparatorChar: The system-dependent path-separator character.  This field is initialized to contain the first character of the value of the system property 'path.separator'.  This character is used to separate filenames in a sequence of files given as a path list. On UNIX systems, this character is ':', on Microsoft Windows systems it is ';'

d.   File.pathSeparator: Same as pathSeparatorChar, but it is a String.

 

Find the below working application.

 

FileConstantsDemo.java

package com.sample.app.files;

import java.io.File;

public class FileConstantsDemo {

  public static void main(String[] args) {

    System.out.println("Operating System: " + System.getProperty("os.name"));

    System.out.println("File.separatorChar = " + File.separatorChar);
    System.out.println("File.separator = " + File.separator);

    System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);
    System.out.println("File.pathSeparator = " + File.pathSeparator);

  }
}

 

Output

Operating System: Mac OS X
File.separatorChar = /
File.separator = /
File.pathSeparatorChar = :
File.pathSeparator = :

 

You may like

file and stream programs in Java

Check whether a directory has some files or not

Check given path is a file and not a symbolic link in Java

Check given path is a directory and not a symbolic link in Java

Convert InputStream to string

Write InputStream to a file in Java

No comments:

Post a Comment