posts - 1,  comments - 25,  trackbacks - 0
          The UML project's code:

          public void cc()
           {
            Properties pro = new Properties();
            try
            {
             File file = new File("./config/cc.properties");
             InputStream in = new BufferedInputStream(new FileInputStream(file)); 
             pro.load(in); 
             System.out.println(pro.getProperty("tu"));
            } catch (IOException e)
            {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            
           }


          This suitation is used for properties file as same level with this class.
          public void cc()
           {
            Properties pro = new Properties();
            try
            {
             pro.load(this.getClass().getResourceAsStream("cc.properties"));
             pro.load(in); 
             System.out.println(pro.getProperty("tu"));
            } catch (IOException e)
            {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            
           }


          使用J2SE API讀取Properties文件的六種方法

          1。使用java.util.Properties類的load()方法
          示例:
          InputStream in = lnew BufferedInputStream(new FileInputStream(name));
          Properties p = new Properties();
          p.load(in);

          2。使用java.util.ResourceBundle類的getBundle()方法
          示例:
          ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

          3。使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)
          示例:
          InputStream in = new BufferedInputStream(new FileInputStream(name));
          ResourceBundle rb = new PropertyResourceBundle(in);

          4。使用class變量的getResourceAsStream()方法
          示例:
          InputStream in = JProperties.class.getResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
          示例:
          InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          6。使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法
          示例:
          InputStream in = ClassLoader.getSystemResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          補(bǔ)充

          Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
          示例:
          InputStream in = context.getResourceAsStream(path);
          Properties p = new Properties();
          p.load(in);

          posted on 2007-05-16 11:00 Daniel 閱讀(212) 評論(0)  編輯  收藏 所屬分類: CoreJava
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 莱芜市| 河北区| 利津县| 微博| 工布江达县| 四子王旗| 肇庆市| 正定县| 连江县| 永春县| 志丹县| 图木舒克市| 龙海市| 南澳县| 屏东县| 沙雅县| 探索| 皋兰县| 两当县| 资兴市| 武胜县| 长岛县| 丰台区| 湘潭市| 台前县| 延吉市| 高阳县| 来宾市| 乌海市| 东莞市| 杭州市| 聂拉木县| 开平市| 文安县| 弋阳县| 东方市| 永寿县| 横山县| 银川市| 天镇县| 遂川县|