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