本站不再更新,歡迎光臨 java開發技術網
          隨筆-230  評論-230  文章-8  trackbacks-0
          如何讀取資源文件:
          (一)
          Properties props = new Properties();   
          props.load(new FileInputStream("db.properties"));
          (二)
          blog.properties文件如下
          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 DBdriver;
            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("不能得到數據庫連接");
                System.out.println("DBdriver: " + DBdriver);
                System.out.println("DBuser: " + DBuser);
                System.out.println("DBpwd: " + DBpwd);
                System.out.println("DBurl: " + DBurl);
                ex.printStackTrace();
              }
              return conn;
            }
          }
          (三)
           Properties props=new Properties();
           props.load(BugFactory.class.getResourceAsStream("xx.properties"));
           String name = props.getPropery("xxxx");
           此時xx.properties應該與該類放在同一個目錄.
          (四)
           ResourceBundle res = ResourceBundle.getBundle("yy.properties");
           String name = res.getString("yyyy");
           yy.properties應放在/WEB-INF/classes目錄
          (五)
          如果你這個Bean打包的話,就把這個文件放在包內。
          我一般是這樣寫的
          Properties prop = new Properties();
          try
          {
           InputStream is = getClass().getResourceAsStream("db.properties");
           prop.load(is);
           if(is!=null)
              is.close();
          }
          另:
          props.load(new FileInputStream("db.properties")); 是讀取當前目錄的db.properties文件
          getClass.getResourceAsStream("db.properties"); 是讀取當前類所在位置一起的db.properties文件
          getClass.getResourceAsStream("/db.properties"); 是讀取ClassPath的根的db.properties文件,注意ClassPath如果是多個路徑或者jar文件的,只要在任意一個路徑目錄下或者jar文件里的根下都可以,如果存在于多個路徑下的話,按照ClassPath中的先后順序,使用先找到的,其余忽略.
          posted on 2007-10-11 23:54 有貓相伴的日子 閱讀(8877) 評論(2)  編輯  收藏 所屬分類: jdk

          評論:
          # re: java讀取資源文件 2007-10-21 15:18 | 有貓相伴的日子
          ClassLoader cl= Thread.getCurrentThread().getContextClassLoader();

          cl.getResourceAsStream("/xx/");
          也是一種讀取文件的方法  回復  更多評論
            
          # re: java讀取資源文件 2007-11-26 14:38 | wenjing
          嘿嘿,很長時間沒有用資源文件了^*^
          筆記也丟失了^*^
          多虧有你的提醒哦
          謝謝拉  回復  更多評論
            
          本站不再更新,歡迎光臨 java開發技術網
          主站蜘蛛池模板: 师宗县| 汉川市| 丹凤县| 确山县| 岳阳县| 南城县| 新郑市| 宜兰市| 凤凰县| 嘉峪关市| 星子县| 新野县| 潜江市| 游戏| 新巴尔虎右旗| 刚察县| 简阳市| 菏泽市| 凌云县| 上杭县| 广饶县| 北辰区| 永仁县| 诏安县| 江源县| 漾濞| 武胜县| 呼玛县| 呈贡县| 广平县| 西充县| 漾濞| 新宁县| 民乐县| 鸡东县| 岑溪市| 金秀| 绥中县| 洛阳市| 沾化县| 绥江县|