Read the Java file path

Source: Internet
Author: User

Remember to learn in the operating system file read in two ways Of course, this is also in each programming language is a generic so java path also divided into relative and absolute path.

Absolute Path

the absolute path URI , which sounds very similar to the URL . 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 in detail . so they have an abstract and inherited relationship. URIAbstract description of theUniform Resource representation symbols, while the URLis the detailed identifier of the way.

How to use it.

If we want to get a detailed location for a class. Just for example, in the following ways:

For example: Test . Class. Getresouce ("");

Suppose the middle adds "/",Test. class. Getresouce ("/"); Gets the Java class followed by the folder.

The two are different.


relative Path

is generally relative to an absolute path , we often use relative to ClassPath.

such as: The use of ClassLoader class getresouce() and other methods. The current classPath relative path is obtained.

URL Ds=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

InputStream In=classloader.getsystemresourceasstream ("Beanx-config.xml");

ClassLoader is a class loader that can specify the loading of special resources . Why the same? . The reason is thatThread.CurrentThread ()sets the currentClassLoader.They both specify the default path.so the two paths are equally. 

Overall Understanding :

URL uri uri is a semantic abstraction that can be absolute and relative. and url relative URL absolute URL


Read the Java file path

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.