Java file path reading and java path reading

Source: Internet
Author: User

Java file path reading and java path reading

Remember that there are two ways to read files in the operating system, of course, this is also common in various programming languages, so the java path is also divided into relative and absolute paths.

Absolute path

The absolute path URI. It looks very similar to the URL. Let's take a look.

URI (Uniformresource Identifier) is a uniform resource Identifier. URL is a uniform resource Identifier. It also describes how to find resources, so they have an abstract and inheritance relationship. The URI abstraction specifies the Uniform Resource representation, while the URL is a specific identifier.

How to use it. For example, we want to obtain the specific position of a class. As long as the method is as follows:

For example:Test. class. GetResouce ("");

If"/", Test. class. GetResouce ("/"); obtains the directory of the java class. The two are different.


Relative Path

It is usually relative to an absolute path. We often use ClassPath.

For example, use the getResouce () method of the ClassLoader class to obtain the relative path of the current classPath.

 URL ds=Thread.currentThread().getContextClassLoader().getResource("");System.out.print(ds+"\n");System.out.print(ClassLoader.getSystemResource(""));

The two results are the same.


To read the content, use the byte stream file directly.

InputStream in=ClassLoader.getSystemResourceAsStream(""beanx-config.xml"");

ClassLoader is a class loader that can be specified to load special resources. why is it the same. the reason is Thread. currentThread () sets the current ClassLoader. they all specify the default path. therefore, the two are in the same path.

Overall understanding:

A URL is a specific URI that not only uniquely identifies a resource, but also provides information for locating the resource. URI is a semantic abstract concept. It can be absolute or relative, while URL must provide sufficient information for locating. Therefore, it is absolute, generally, the relative URL is targeted at another absolute URL, which is basically absolute.

 



How can I obtain the relative path when writing a file in java?

Jar files can only be read and cannot be written.

How does java obtain the path in the browser file box?

In jsp, give the text an id, and then give the test button an event in jsp. Click it to get the text field value through getElementById (id), and then pass it to the background.

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.