<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-9  評論-15  文章-1  trackbacks-0
        用Java寫properties文件時,如果直接用setProperties和store方法往FileOutputStream寫,寫出來的東西面目全非了。沒有正確的格式。因此我使用了這個方法來寫,一行一行的讀。一行一行地寫。
      1/**
      2   * write property.
      3   * @param title parameter defined in properties file
      4   * @param key parameter defined title value
      5   * @return String return value
      6   */

      7  String writeProp(String filePath, String fileName, String title, String key,
      8                   Logger logger) {
      9
     10    String strResult = "";
     11    String pathAddFile = ""//write file with path and name
     12    String tempFile = "";
     13    String strTemp = ""//use for identify if the modify is success
     14    //filePath is null the file in the default path ,else file in the filePath+\+fileName
     15    if (filePath.equals("")) {
     16      pathAddFile = fileName;
     17      tempFile = "temp.properties";
     18    }

     19    else {
     20      pathAddFile = filePath + systemSeparator + fileName;
     21      tempFile = filePath + systemSeparator + "temp.properties";
     22    }

     23    //properties file
     24    File aFile = new File(pathAddFile);
     25    //temp file
     26    File tFile = new File(tempFile);
     27    if (!aFile.exists()) {

     31      strResult = "error";
     32      return strResult;
     33    }

     34    //set property to properties
     35    try {
     36      FileReader fr = new FileReader(pathAddFile);
     37
     38      BufferedReader br = new BufferedReader(fr);
     39      try {
     40        FileWriter fw = new FileWriter(tempFile);
     41        PrintWriter out = new PrintWriter(fw);
     42
     43        String strLine = br.readLine().trim();
     44        while (strLine != null{
     45          //identify if strLine have title,have change key
     46          if (strLine.startsWith(title)) {
     47            strLine = title + "=" + key;
     48            strTemp = "1";
     49          }

     50          out.write(strLine);
     51          out.println();
     52          out.flush();
     53          //read next line
     54          strLine = br.readLine();
     55        }

     56        fw.close();
     57        out.close();
     58        //close BufferedReader object
     59        br.close();
     60         //close file
     61        fr.close();
     62        //delete properties file
     63        if (aFile.exists()) {
     64          if (!aFile.delete()) {
     68            return "error";
     69          }

     70        }

     71        //rename temp file to properties file
     72        if (!tFile.exists()) {
     76          return "error";
     77        }

     78        tFile.renameTo(aFile);
     79        if (!strTemp.equals("1")) {
     80          //there is no title prop exit so modify failed
     85          strResult = "error";
     86
     87        }

     88        return strResult;
     89      }

     90      catch (IOException ex2) {
     91        ex2.printStackTrace();
     92        strResult = "error";
     93        logger.fatal(
     94            "CmnEToyotaExtractProp ----- writeProp ----- failed !");
     95        return strResult;
     96
     97      }

     98    }

     99    catch (FileNotFoundException ex1) {
    100      ex1.printStackTrace();

    103      strResult = "error";
    104      return strResult;
    105    }

    106
    107  }
       該方法實現了對屬性文件參數的修改。
    posted on 2005-08-02 15:41 jam 閱讀(5182) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 精品免费国产一区二区三区| 久久久高清日本道免费观看| 无人影院手机版在线观看免费| 亚洲免费福利视频| 亚洲综合免费视频| 亚洲欧洲日产国产最新| 日本免费网站视频www区| 亚洲午夜成激人情在线影院| 波多野结衣在线免费观看| 亚洲大香伊人蕉在人依线| 亚洲人成网站免费播放| 亚洲精华液一二三产区| 全部免费毛片免费播放| 国产一区二区三区免费观在线| 国产日产亚洲系列最新| 久久成人免费电影| 亚洲午夜精品一区二区公牛电影院| 亚洲中文无码永久免费 | 亚洲高清在线mv| 亚色九九九全国免费视频| 亚洲美国产亚洲AV| 亚洲国产小视频精品久久久三级| 中文无码日韩欧免费视频| 亚洲AV日韩AV永久无码下载| 在线视频精品免费| 美女隐私免费视频看| 国产成人A亚洲精V品无码| 91大神免费观看| 亚洲av无码专区在线观看亚| 亚洲美女又黄又爽在线观看| 亚洲精品在线免费看| 亚洲av无码一区二区三区在线播放| 亚洲男人的天堂一区二区| 色欲国产麻豆一精品一AV一免费 | 亚洲日本韩国在线| 久久国产色AV免费看| 精品国产亚洲AV麻豆| 亚洲国产高清人在线| 国产一区二区免费在线| 最近高清中文字幕免费| 美女视频黄频a免费观看|