Java Static resource access detailed

Source: Internet
Author: User

A. Java Project project

Get resource files If Project is Java project

1, if it is in the root directory of the project directly access

FileReader FR = new FileReader ("Config.properties");

2, can also be directly based on the actual path to access

FileReader FR = new FileReader ("D:\\practice\\backend\\myeclipse\\web_db\\config.properties");

3, using the class loader access (where the directory is the SRC directory if you want to put in a specific package in the directory to add the package)

InputStream fr = JDBCUtil.class.getClassLoader (). getResourceAsStream ("config.properties");

Second, Web project
Properties pt = new properties ();p t.load (New FileReader ("Config.properties"));

If you follow the code above (the file or the root directory), the following error is reported

Workaround for the above error appears

1. Access resources in the same way as the ClassLoader (in the SRC directory)

InputStream is = JDBCUtil.class.getClassLoader (). getResourceAsStream ("config.properties");

2. Access resources using the Internationalized resource class

ResourceBundle rb=resourcebundle.getbundle ("Cn.itcast.jdbc.util.config");

But if you want to go through the browser in the root directory of the project, you can't access the resources.

Because the release of the project is published in the Webroot Directory of Things (the root directory of resources will not be published in the project, so cannot access resources) Tomcat compiled files such as

If you want to access the resource file through such a path http://localhost:8080/web_servlet03/WEB-INF/classes/config.properties is also inaccessible because the Web-inf directory is not available through

accessed by the browser. If you do want to access the resource file, you must put the resource file in the Webroot directory to directly access the following:

Iii. servlet access to resources

1, you can use the class loader

InputStream Is=pathdemo.class.getclassloader (). getResourceAsStream ("A.txt");

2. Use ServletContext context to access resources (note: Access to the resources can not be placed in the root directory because when the release is published in the Webroot below, read the resources when reading the server things)

ServletContext sc = Getservletcontext (); String Bpath=sc.getrealpath ("/b.txt");//Get real path based on virtual path

Java Static resource access detailed

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.