How to read a resource file: (i) Properties props = new properties ();
Props.load (New FileInputStream ("Db.properties"));
(ii) Blog.properties documents are as follows Dbdriver=oracle.jdbc.driver.oracledriver Dburl=jdbc:oracle:thin:@127.0.0.1:1521:ora92 Dbuser=blog dbpwd=blog--------------public class Config {public config () {this.initdbconfig ().} Public String DBd
River
Public String Dbuser;
Public String dbpwd;
Public String Dburl;
private void Initdbconfig () {try {resourcebundle bundle = resourcebundle.getbundle ("blog");
Dbdriver = bundle.getstring ("Dbdriver");
Dburl = bundle.getstring ("Dburl");
Dbuser = bundle.getstring ("Dbuser");
Dbpwd = bundle.getstring ("Dbpwd");
catch (Exception ex) {ex.printstacktrace ();
----------------public class DAO {public dao () {} public Connection getconnection () {Connection conn = null;
Config config = new config (); String dbdriver = config.
Dbdriver; String dbuser = config.
Dbuser; String dbpwd = CoNfig.
Dbpwd; String dburl = config.
Dburl;
try {class.forname (dbdriver);
conn = Drivermanager.getconnection (Dburl, Dbuser, dbpwd);
catch (Exception ex) {System.out.println ("********************");
SYSTEM.OUT.PRINTLN ("Cannot get database connection");
System.out.println ("Dbdriver:" + dbdriver);
System.out.println ("Dbuser:" + dbuser);
System.out.println ("dbpwd:" + dbpwd);
System.out.println ("Dburl:" + dburl);
Ex.printstacktrace ();
Return conn;
} (iii) Properties Props=new properties ();
Props.load (bugfactory. Class.getresourceasstream ("xx.properties"));
String name = Props.getpropery ("xxxx");
At this point xx.properties should be placed in the same directory as the class. (iv) ResourceBundle res = Resourcebundle.getbundle ("yy.properties");
String name = res.getstring ("yyyy");
Yy.properties should be placed in the/web-inf/classes directory (v) If you pack this bean, put the file in the package.
I usually write this. Properties prop = new properties (); try {InputStream is = GetClass (). getResourceAsStream (' db.properties '); Prop.load (IS); if (is!=NULL) Is.close (); Another: Props.load (new FileInputStream ("Db.properties")); Is the db.properties file that reads the current directory Getclass.getresourceasstream ("Db.properties"); is the Db.properties file Getclass.getresourceasstream ("/db.properties") that reads the current class location. is to read the root of the classpath db.properties file, note that classpath if it is multiple paths or jar files, as long as in any path directory or in the jar file under the root can be, if the existence of multiple paths, According to the order in the Classpath, use the first found, the rest ignored
Turn from: http://blog.csdn.net/kjfcpua/article/details/4888899