posts - 4, comments - 1, trackbacks - 0, articles - 2

          2007年7月6日

          有新發(fā)現(xiàn) 發(fā)現(xiàn)一個firefox的插件 具有把http資源解析成PDF的功能 先研究一下這個東西

          posted @ 2007-07-23 09:41 黃鵬霄 閱讀(274) | 評論 (1)編輯 收藏

          有個朋友要用getClass().getResourceAsStream() 提取test.properties

          但是在服務(wù)器運行過程中 無論怎么更改test.properties

          得出的數(shù)據(jù)還是最初的那個

          我后來試了一下 代碼如下

          package test;

          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.util.Properties;

          public class Test {
              Properties pp 
          = null;

              
          public Properties getData() throws IOException {
                  InputStream is 
          = getClass().getResourceAsStream("/test.properties");
                  
          // InputStream is = new FileInputStream(
                  
          // "D:\\java\\apache-tomcat-5.5.17\\apache-tomcat-5.5.17\\webapps\\testp\\WEB-INF\\classes\\test.properties");
                  System.out.println(is.hashCode());
                  pp 
          = new Properties();
                  pp.load(is);
                  System.out.println(pp.hashCode());
                  
          // Properties pp = System.getProperties();
                  
          // Enumeration<String> enu = (Enumeration<String>) pp.propertyNames();
                  
          // while(enu.hasMoreElements()){
                  
          // String name = enu.nextElement();
                  
          // System.out.println(name + "=" +pp.getProperty(name));
                  
          // }
                  
          // is.close();
                  is.close();
                  
          return pp;
              }

              
          public static Properties getProperties() {
                  
          try {
                      
          return new Test().getData();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          return null;
              }

              
          public static void main(String[] args) {
                  
          while (true) {
                      System.out.println(getProperties());
                      
          try {
                          Thread.sleep(
          1000L);
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
          上面這個解析的Properties pp 的hashcode 始終不變

          而InputStream 的hash 缺一直改變

          由此可以推斷結(jié)論有
          getClass().getResourceAsStream() 是ClassLoader 加載Class一樣的把test.properties 加載進了內(nèi)存


          但是針對上面的紅字我寫了MAIN函數(shù)做為測試

          現(xiàn)在發(fā)現(xiàn)如果我更改Properties ,會立刻做出反應(yīng)

          main函數(shù)中的代碼我的理解如下 每次ClassLoader都在加載ClassPath下的文件,當(dāng)發(fā)現(xiàn)改變就構(gòu)成Properties 改變

          所以我想這是不是tomcat的ClassLoader 的一個bug ??

          以上言論,均屬我的猜測,還望高手指點.

          新發(fā)現(xiàn):

            InputStream is = Test.class.getClassLoader().getResourceAsStream(// .getResourceAsStream(
            InputStream is = Test.class.getResourceAsStream(// .getResourceAsStream(
          這樣加載的Properties 是兩個不同的實例 我的意思是想說兩份不同的內(nèi)存

          所以如果想test.properties隨時變 用絕對路徑 InputStream is = new FileInputStream("絕對路徑")
          就可以了

          posted @ 2007-07-06 00:08 黃鵬霄 閱讀(3446) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 怀宁县| 稷山县| 龙州县| 正安县| 杭州市| 巴中市| 南投县| 龙岩市| 旬邑县| 金平| 克拉玛依市| 贵州省| 阿坝县| 江门市| 邵东县| 织金县| 江阴市| 齐河县| 福海县| 乌兰察布市| 南部县| 莱西市| 吉林省| 耿马| 桂东县| 姜堰市| 黄平县| 凌海市| 吐鲁番市| 龙岩市| 中西区| 赤壁市| 且末县| 山阴县| 诸城市| 舒城县| 邵东县| 禄丰县| 大竹县| 冷水江市| 获嘉县|