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)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 西昌市| 白城市| 吴忠市| 神农架林区| 巩义市| 上高县| 鄂托克旗| 山东| 双峰县| 富源县| 固镇县| 会东县| 达日县| 博野县| 安国市| 杭州市| 灌阳县| 营山县| 鹤峰县| 定陶县| 汨罗市| 汶上县| 双鸭山市| 武义县| 台北县| 阳江市| 伊川县| 甘南县| 湟源县| 合作市| 信丰县| 阿图什市| 天峻县| 汤原县| 偃师市| 区。| 萝北县| 邓州市| 桂东县| 富民县| 黎城县|