The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          讀屬性文件
          Properties prop = new Properties();
          InputStream in = getClass().getResourceAsStream("/IcisReport.properties");
          prop.load(in);
          Set keyValue = prop.keySet();
          for (Iterator it = keyValue.iterator(); it.hasNext();)
          {
          String key = (String) it.next();
          }
          ------------------------
          outputFile = new FileOutputStream(fileName);
          propertie.store(outputFile, description);
          outputFile.close();
          -----------------------------------------------------------------------------------------
          Class.getResourceAsStream ("/some/pkg/resource.properties");
          ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
          java.util.ResourceBundle rs = java.util.ResourceBundle.getBundle("some.pkg.resource");
          rs.getString("xiaofei");
          -----------------------------------------------------------------------------------------
          寫屬性文件
          Configuration saveCf = new Configuration();
          saveCf.setValue("min", "10");
          saveCf.setValue("max", "1000");
          saveCf.saveFile(".\config\save.perperties","test");

          總結:java的properties文件需要放到classpath下面,這樣程序才能讀取到,有關classpath實際上就是java類或者庫的存放路徑,在java工程中,properties放到

          class文件一塊。在web應用中,最簡單的方法是放到web應用的WEB- INF\classes目錄下即可,也可以放在其他文件夾下面,這時候需要在設置classpath環境變量的

          時候,將這個文件夾路徑加到 classpath變量中,這樣也也可以讀取到。在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變


          量,WEB-INF\classes其實也是,java工程的class文件目錄也是。

          發個例子大家自己看哈.
          package control;

          import java.io.BufferedInputStream;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.Enumeration;
          import java.util.Properties;

          public class TestMain {
           
           //根據key讀取value
           public static String readValue(String filePath,String key) {
            Properties props = new Properties();
                  try {
                   InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                   props.load(in);
                   String value = props.getProperty (key);
                      System.out.println(key+value);
                      return value;
                  } catch (Exception e) {
                   e.printStackTrace();
                   return null;
                  }
           }
           
           //讀取properties的全部信息
              public static void readProperties(String filePath) {
               Properties props = new Properties();
                  try {
                   InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                   props.load(in);
                      Enumeration en = props.propertyNames();
                       while (en.hasMoreElements()) {
                        String key = (String) en.nextElement();
                              String Property = props.getProperty (key);
                              System.out.println(key+Property);
                          }
                  } catch (Exception e) {
                   e.printStackTrace();
                  }
              }

              //寫入properties信息
              public static void writeProperties(String filePath,String parameterName,String parameterValue) {
               Properties prop = new Properties();
               try {
                InputStream fis = new FileInputStream(filePath);
                      //從輸入流中讀取屬性列表(鍵和元素對)
                      prop.load(fis);
                      //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
                      //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
                      OutputStream fos = new FileOutputStream(filePath);
                      prop.setProperty(parameterName, parameterValue);
                      //以適合使用 load 方法加載到 Properties 表中的格式,
                      //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
                      prop.store(fos, "Update '" + parameterName + "' value");
                  } catch (IOException e) {
                   System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
                  }
              }

              public static void main(String[] args) {
               readValue("info.properties","url");
                  writeProperties("info.properties","age","21");
                  readProperties("info.properties" );
                  System.out.println("OK");
              }


           發個例子大家自己看哈.

          package control;

          import java.io.BufferedInputStream;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.util.Enumeration;
          import java.util.Properties;

          public class TestMain {
           
           //根據key讀取value
           public static String readValue(String filePath,String key) {
            Properties props = new Properties();
                  try {
                   InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                   props.load(in);
                   String value = props.getProperty (key);
                      System.out.println(key+value);
                      return value;
                  } catch (Exception e) {
                   e.printStackTrace();
                   return null;
                  }
           }
           
           //讀取properties的全部信息
              public static void readProperties(String filePath) {
               Properties props = new Properties();
                  try {
                   InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                   props.load(in);
                      Enumeration en = props.propertyNames();
                       while (en.hasMoreElements()) {
                        String key = (String) en.nextElement();
                              String Property = props.getProperty (key);
                              System.out.println(key+Property);
                          }
                  } catch (Exception e) {
                   e.printStackTrace();
                  }
              }

              //寫入properties信息
              public static void writeProperties(String filePath,String parameterName,String parameterValue) {
               Properties prop = new Properties();
               try {
                InputStream fis = new FileInputStream(filePath);
                      //從輸入流中讀取屬性列表(鍵和元素對)
                      prop.load(fis);
                      //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
                      //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
                      OutputStream fos = new FileOutputStream(filePath);
                      prop.setProperty(parameterName, parameterValue);
                      //以適合使用 load 方法加載到 Properties 表中的格式,
                      //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
                      prop.store(fos, "Update '" + parameterName + "' value");
                  } catch (IOException e) {
                   System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
                  }
              }

              public static void main(String[] args) {
               readValue("info.properties","url");
                  writeProperties("info.properties","age","21");
                  readProperties("info.properties" );
                  System.out.println("OK");
              }
          }
          posted on 2013-05-17 09:51 Eric_jiang 閱讀(209) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 临沂市| 上林县| 东兴市| 宁南县| 齐齐哈尔市| 伊川县| 灵台县| 交口县| 延寿县| 修武县| 潞西市| 玛沁县| 唐海县| 兴城市| 广水市| 大田县| 溧水县| 洪泽县| 阳山县| 嘉兴市| 宜兰县| 汉中市| 炉霍县| 常山县| 米脂县| 都昌县| 阿拉善盟| 景泰县| 普格县| 晋州市| 夹江县| 成安县| 肇东市| 仁怀市| 定西市| 中西区| 铅山县| 汝南县| 南溪县| 博罗县| 广饶县|