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類的構造函數
          示例:
          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()靜態方法
          示例:
          InputStream in = ClassLoader.getSystemResourceAsStream(name);
          Properties p = new Properties();
          p.load(in);

          補充

          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 閱讀(216) 評論(0)  編輯  收藏 所屬分類: CoreJava
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 房山区| 呼和浩特市| 思南县| 明星| 内丘县| 沈丘县| 巴林右旗| 交城县| 武平县| 武鸣县| 崇义县| 班玛县| 云林县| 寻乌县| 罗源县| 庆云县| 从江县| 芦山县| 招远市| 龙海市| 双辽市| 板桥市| 普定县| 遂溪县| 龙井市| 佛学| 兴文县| 宿州市| 江油市| 西丰县| 错那县| 原平市| 科尔| 绥滨县| 绥德县| 鸡东县| 海盐县| 化隆| 琼中| 大兴区| 景东|