Core Java >> I/O


Get file separator symbol on windows and linux


Every operating system has its own way of separating the paths. Some may use / while others may use as the separator character. For e.g UNIX has a path like /usr/home but Windows will have paths like c:test.txt

When File I/O operations are performed a Java application then they need to run on any kind of operating system. To resolve this issue of file path separator character, Java provides special properties which represent the underlying operating system’s path separator characters.

The following program shows how the separate character can be used in a Java application.

//9FCWJTTGDMQP
package com.example;

import java.io.File;

/** This class is a Java tutorial for separator and path separator
 *
 * @author Extreme Java
 */
public class Test{

	/** This method shows how to show the usage of File.separator property
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		File f = new File("test" + File.separator + "file");
		System.out.println(f.getAbsolutePath());
	}
}

output:
C:UsersworkspaceTesttestfile

Here, we didn’t specify any separator character and instead used the separator property of the File class which automatically detects the corresponding character for the operating system on which the program is running.

The output of above program will change for UNIX operating system.

Leave Comment