Read the file path of Java research

Source: Internet
Author: User



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

Related Article

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.