Share on Google+Share on Google+

Java file separator

Advertisement
This section demonstrates you the use of file separator.

Java file separator

This section demonstrates you the use of file separator.

A file separator is a character that is used to separate directory names that make up a path to a particular location. It is operating system dependent. On Microsoft Windows,  it is a back-slash character (\) while on Mac OS and unix based operating system, it is forward slash(/).  By using the file.separator key from the system property, you can avoid checking for the OS.

In the given example, we have used file.separator to make up a path.

Here is the code:

import java.io.*;

public class PathSeparator {
	public static void main(String[] args) {
		String path = "C:";
		String pathSep = path + File.separator + "Hello" + File.separator;
		System.out.println(pathSep);
	}
}

Output:

C:\Hello\

Advertisements

Advertisement

Posted on: June 5, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.