How to specify the path when the java program reads the resource file?

Source: Internet
Author: User

How to specify the path when the java program reads the resource file?

Java program, there are two ways to read resources.

First, it is directly carried out through File. For example, FileReader and BufferedReader. The file directory is relative to the Project. For example, if I create a new MyProject, I have an icons folder under MyProject. to read the content in icons, the path is like "icons /***. the relative path of gif.

The second is obtained through getClass (). getResource. The second method must ensure that the file is in the path included in the classpath. The general classpath (you can view the. classpath file of any eclipse project) includes at least the following content:






We can see that src and bin are default, so the second method is that the resource files must be in these two folders. Therefore, if there is a property. ini file in the src folder, you can use getResource ("/property. ini"). Note that it must start.


The maven project has a resource folder by default, which is located in the classpath path. Therefore, the resource files are usually put here, And the content can be conveniently obtained through getResource.


Java specially designed a class for processing configuration information such as xxx = xxx, ResourceBundle. The general processing method is as follows,

ResourceBundle resource = ResourceBundle. getBundle ("conf/a.txt ");

PropertyResourceBundle bundle = (PropertyResourceBundle) bundle;

Enumeration Keys = bundle. keys ();

For (String key: keys)

{

// Solve bundle. getString (key)

}

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.