Remember that you learned in the operating system There are two ways to read a file , Of course, this is common in every programming language. , so Java The path is also divided , relative and absolute paths. In the previous chapter, we shared the learning design pattern of Java research-a detailed description of the combination pattern. Interested friends can go and see.
Absolute Path
absolute path Span lang= "en-US" style= "Font-family:calibri" >uri Span lang= "ZH-CN" style= "Font-family:simsun" > Listen and url Span lang= "ZH-CN" style= "Font-family:simsun" > very similar, Then let's take a look at it.
URI(uniformresource Identifier) Uniform Resource Identifier . The URL Uniform Resource Locator , which is a locator , also explains how to find resources . so they have an abstract and inherited relationship. URIAbstract description of theUniform Resource representation symbols, while the URLis the specific identifier of the way.
How to use it. If we want to get the exact location of a class. As long as the following methods:
For example: Test . Class. Getresouce ("");
If "/"is added in the middle,test. class. Getresouce ("/"); Gets the directory of the Java class. The two are different.
relative Path
is usually relative to an absolute path , we often use it in relation to ClassPath .
such as: Using the ClassLoader class of getresouce() and other methods, get is the current ClassPath relative path.
123 |
URLDS=Thread.CurrentThread().Getcontextclassloader().GetResource("");System. out.Print(DS+"\ n");System. out.Print(ClassLoader.Getsystemresource("")); |
Two kinds of results are the same .
to read content , directly using a byte stream file
1 |
InputStream inch = ClassLoader . Getsystemresourceasstream (""beanx-config. XML "" ); |
ClassLoader is a class loader that can specify the loading of special resources . Why is it the same? . The reason is thatThread.CurrentThread ()sets the currentClassLoader.They both specify the default path.so the two paths are the same.
General Knowledge :
URL Span lang= "ZH-CN" style= "Font-family:simsun" is a specific uri uri is a semantically abstract concept that can be absolute or relative, while url must provide enough information to locate, so, is absolute, and usually said Relative URL absolute URL