About Java's file.separator

Source: Internet
Author: User

The path separators under windows are not the same as the path separators under Linux, and when you use absolute paths directly, cross-platform bursts out "No such file or diretory" exceptions.

For example, to create a test.txt file under the temp directory, which should be written under Windows:
File File1 = new file ("C:\tmp\test.txt");
Under Linux, this is the case:
File File2 = new file ("/tmp/test.txt");

If you want to consider cross-platform, it's best to write this:
File MyFile = new file ("C:" + file.separator + "tmp" + file.separator, "test.txt");

The file class has several static fields similar to separator, which are system-related and should be used as much as possible when programming.

Separatorchar

public static final Char Separatorchar

The system-related default name delimiter. This field is initialized to the first character that contains the system attribute File.separator value. On a UNIX system, the value of this field is '/'; on a Microsoft Windows system, it is ' \ '.

Separator

public static final String separator

The system-related default name delimiter, which is represented as a string for convenience. This string contains only one character, that is, Separatorchar.

Pathseparatorchar

public static final Char Pathseparatorchar

The system-related path delimiter. This field is initially the first character that contains the system attribute Path.separator value. This character is used to separate the file names in a sequence of files given as a list of paths. On a UNIX system, this field is ': '; on a Microsoft Windows system, it is '; '.

PathSeparator

public static final String PathSeparator

The system-related path delimiter, for convenience, is represented as a string. This string contains only one character, that is, Pathseparatorchar.

About Java's file.separator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.