Path basics In the Java language (always forgotten, recorded)

Source: Internet
Author: User
Tags file separator
Path knowledge in Java

First, line break
A newline (\ n) is the cursor move down one line but does not move to the beginning of this line.

Two, carriage return character
Carriage return (\ r) is to return to the beginning of the current line without moving the line down.
Enter key will be executed after \n\r this is what we see the general meaning of the carriage return, so you use the 16 file to view a text, will be found at the end of the "\ r \ n".

Third, Tab
tab is a tab character, that is, "\ T", which is reserved for 8-character display widths for alignment.

Iv. system.getproperties () current system Properties
1) file.separator
The file separator, which is "\" in the Windows system and is "/" in the UNIX system.
You can also get this in your code:
Java code System.out.println ("File separator:" +system.getproperty ("File.separator")); System.out.println ("File separator:" +file.separator);

2) Path.separator
Path separator, in Windows system is ";" That is, the semicolon, in the UNIX system is ":" That is, a colon.
You can also get this in your code:
Java code System.out.println ("Path separator:" +system.getproperty ("Path.separator")); SYSTEM.OUT.PRINTLN ("Path separator:" +file.pathseparator);

3) Line.separator
Line delimiter, which is "\ r \ n" in the Windows system, and "n" is the newline character in the Unix system.
You can also get this in your code:
Java code System.out.println ("line delimiter:" +system.getproperty ("Line.separator"));

4) User.Name
User's account name

5) User.home
User's home directory

6) User.dir
User's current working directory
You can also get this in your code:
Java Code System.out.println (System.getproperty ("User.dir"));

V. Class.getResource and Classloader.getresource usage and difference
Engineering root Directory
|-com.cn.test
|-test.class
|-test2.txt
|-test1.txt

1 when using the Class.getResource method to obtain resources, there are two ways of path writing.
Java code URL URL1 = This.getclass (). GetResource ("Test2.txt");
And
Java code/Note in this way, the path must begin with "/" and the URL url2 = This.getclass () is found from the engineering root directory. GetResource ("/com/cn/test/test2.txt");

When the This.getclass (). GetResource ("Test2.txt") is used, the GetResource method automatically adds the package path of the current class to the path Test2.txt, for example: com/cn/test/ Test2.txt, and then look for resources based on this path.

When the This.getclass (). GetResource ("/com/cn/test/test2.txt") is used, the GetResource method automatically/com/cn/test/test2.txt the path to the front "/ "Remove, change to Com/cn/test/test2.txt, and then follow this path to find."

That is to say, the Class.getResource method either looks up according to the current classpath or is an absolute path, looking from the root directory.

2 The use of Classloader.getresource method to obtain resources, there is no such trouble above, only one way of writing.
Java code//note in this writing, there is no "/" This.getclass (). getClassLoader (). GetResource ("Test1.txt") in front of the path;
The path to the Classloader.getresource method does not need to start with "/", but the method defaults to the start of the project root directory.

So it seems that the use of the Classloader.getresource method is the same as that of This.getclass (). GetResource ("/com/cn/test/test2.txt").

Why, then? Let's take a look at the source code for the This.getclass () GetResource method:
Java Code Public Java.net.URL getresource (String name) {name = ResolveName (name);          ClassLoader cl = GetClassLoader0 ();              if (cl==null) {//A system class.          return Classloader.getsystemresource (name);      return Cl.getresource (name); }

The main task of the ResolveName method is to give the name "/" at the beginning of the "/" and remove "/". To the beginning of the "/", increase the package path for the current class. For example, when name is/com/cn/test/test2.txt, after the ResolveName method is processed, it becomes com/cn/test/test2.txt.

The general class is loaded by the System class loader, i.e. the ClassLoader CL obtained by the GetClassLoader0 () method is generally empty.

This leads to the fact that the method is actually a resource found by the GetResource (name) method of the System class loader obtained by the Classloader.getsystemresource (name) method. That is, a resource that is found through the Classloader.getresource ("Com/cn/test/test2.txt") method.

This just explains the Classloader.getresource ("Com/cn/test/test2.txt") and This.getclass (). GetResource ("/com/cn/test/test2.txt") Can find the reason for Test2.txt.

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.