Java reading resource files in N-way

Source: Internet
Author: User
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

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.