Java path Two ways of writing "/" and "\ \"

Source: Internet
Author: User


1. Example:

String path="d:\\ new Folder \\2.png";

File file=new file (path);

System.out.println (File.exists ());

String path1="d:/new Folder/2.png";

File file1=new file (path);

System.out.println (File1.getabsolutepath ());

System.out.println (File1.getcanonicalpath ());



2.
The specific use can achieve access to the effect of the path, but there is a small difference between the forward slash, generally in the configuration file path, pointing to the next path as long as the use of one example: "C:/a/1.txt", and the backslash, in the configuration file path, because it has a special meaning in Java, As escape characters exist, so the specific meaning of the backslash to two for example: "C\\a\\1.txt"; here the first backslash is as an escape character, the second is the true meaning of a backslash
It is generally assumed that the function of "/" is equivalent to "\ \" in Java path generally used "/" Path in Windows General "\" Linux, Unix path generally used "/"
It's best to use "/"  because Java is cross-platform. "\" (in Java code should be \ \) is a path delimiter under Windows, and Linux and Unix are "/". "/" can also be recognized under Windows. So it's better to use "/"

Java path Two ways of writing "/" and "\ \"

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.