Introduction to File. separator separators (Java ),

Source: Internet
Author: User

Introduction to File. separator separators (Java ),

In windows, the path separator is different from the path Separator in Linux. When the absolute path is used directly, baolu across platforms will expose "no suchffile or directory ", this is often caused by incorrect path delimiters in the code.

For example, if you create a test.txt file under the tempdirectory, you should write the file in windows as follows:

File file1 = new File ("c: \ temp \ test.txt ");

In Linux, it is like this:

File file2 = new File ("/tmp/test.txt ")

If you want to consider cross-platform, it is best to write as follows:

File myFile = new File ("C:" + File. separator + "temp" + File. separator + "test.txt ")

The File class has several static fields similar to the separator, which are related to the system and should be used as much as possible during programming.

File. separatorChar

Public static final charString separator

Default name separator related to the system. This field is initialized to contain the first character of the system property file. separator value. On UNIX systems, the value of this field is '/'; on Microsoft Windows systems, it is '\'.

File. separator

Public static final String separatorcharseparatorChar

Default name separator related to the system. For convenience, it is represented as a string. This string contains only one character, namely separatorChar.

File. pathSeparatorChar

Public static final char pathSeparatorChar

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

File. pathSeparator

Public static final String pathSeparator

System-related path separator, which is expressed as a string for convenience. This string contains only one character, namely pathSeparatorChar.

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.