posts - 30,  comments - 50,  trackbacks - 0
          最笨最實在思路最全面的做法,用于最基礎的代碼鍛煉非常有效,推薦給大家打打java底^_^
          package cn.net.withub.xfgl.common; 

          /** 
          @author YuLuo 
          */
           

          import java.io.File; 
          import java.io.FileInputStream; 
          import java.io.FileNotFoundException; 
          import java.io.FileOutputStream; 
          import java.io.IOException; 
          import java.io.InputStream; 
          import java.util.Properties; 
          //前一篇已介紹此方法 
          import cn.net.withub.xfgl.common.ToUnicode; 

          public class CreatePro 
          //讀文件 
          public static String readFile(String filePath, String parameterName) throws IOException, FileNotFoundException 
              String result 
          = null
              File file 
          = new File(filePath); 
                 
          if (file.exists()) 
                  FileInputStream fis 
          = new FileInputStream(file); 
                 
          byte[] b = new byte[fis.available()]; 
                  fis.read(b); 
                  result 
          = new String(b, "UTF-8"); 
                  fis.close(); 
              }
           
             
          return result; 
          }
           
          //修改后存儲 
          public static void saveFile(String content, String path, String fileName) throws IOException 
              File f 
          = new File(path); 
             
          if (!f.exists()) 
                  f.mkdirs(); 
              }
           
              File fn 
          = new File(f, fileName); 
              FileOutputStream fos 
          = new FileOutputStream(fn); 
              fos.write(content.getBytes()); 
              fos.close(); 
          }
           
          //刪除舊文件 
          public static void deleteFile(String path) throws IOException 
              File f 
          = new File(path); 
             
          if (f.exists()) 
                  f.delete(); 
          }
           else 
                 
          throw new IOException("未找到相關文件"); 
              }
           
          }
           
          //執(zhí)行方法 
          public static boolean writeProperties(String filePath, String parameterName, String parameterValue) 
             
          boolean flag = false
             
          try 
                 
          //取得文件所有內(nèi)容 
                 
          String all = CreatePro.readFile(filePath, parameterName); 
                  String result 
          = null
                 
          //如果配置文件里存在parameterName 
                 
          if (all.indexOf(parameterName) != -1
                     
          //得到parameterName前的字節(jié)數(shù) 
                     
          int a=all.indexOf(parameterName); 
                     
          //取得以前parameterName的值 
                     
          String old = readProperties(filePath, parameterName); 
                     
          //得到parameterName值前的字節(jié)數(shù) 
                     
          int b=a+(parameterName.length()+"=".length()); 
                     
          //新的properties文件所有內(nèi)容為:舊的properties文件中內(nèi)容parameterName+"="+新的parameterName值(parameterValue)+舊的properties文件中parameterName值之后的內(nèi)容 
                     
          result=all.substring(0,a)+parameterName+"="+parameterValue+all.substring(b+ToUnicode.convert(old).length(),all.length()); 
                  }
           
                 
          //刪除以前的properties文件 
                
          CreatePro.deleteFile(filePath); 
                 
          //存儲新文件到以前位置 
                 
          String[] arrPath = filePath.split("WEB-INF"); 
                  String path 
          = arrPath[0]+"WEB-INF\\configs"
                  CreatePro.saveFile(result, path, 
          "xf.properties"); 
                  flag
          =true
          }
           catch (IOException e) 
                  e.printStackTrace(); 
                  flag
          =false
              }
           
             
          return flag; 
          }
           
          //Properties方法寫properties文件 
          /*Properties prop = new Properties(); 
          try { 
          InputStream fis = new 
          FileInputStream(filePath); 
          //從輸入流中讀取屬性列表(鍵和元素對) prop.load(fis); 
          //調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 
          //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結果。 
          OutputStream fos = new FileOutputStream(filePath); 
          prop.setProperty(parameterName,parameterValue); 
          //以適合使用 load 方法加載到 Properties 表中的格式, 
          //將此Properties 表中的屬性列表(鍵和元素對)寫入輸出流 
          prop.store(fos, "Update '" + parameterName + "' value \n"); 
          falg = true; 
          }catch (IOException e) { 
          e.printStackTrace(); 
          System.err.println("Visit " + filePath + " for updating " + parameterName + " value error"); 
          falg = false; 
          }
          */
           
          //讀properties文件,Properties方法 
          public static String readProperties(String filePath, String parameterName) 
              String value 
          = ""
              Properties prop 
          = new Properties(); 
             
          try 
                  InputStream fis 
          = new FileInputStream(filePath); 
                  prop.load(fis); 
                  value 
          = prop.getProperty(parameterName); 
              }
           catch (IOException e) 
                  e.printStackTrace(); 
              }
           
             
          return value; 
          }
           
          posted on 2007-07-31 16:24 心。 閱讀(2470) 評論(2)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 博罗县| 扶余县| 章丘市| 重庆市| 威海市| 陵川县| 抚宁县| 伊川县| 泰兴市| 安徽省| 延寿县| 金坛市| 永嘉县| 肥东县| 克拉玛依市| 泰顺县| 青海省| 大新县| 杨浦区| 泊头市| 两当县| 旌德县| 邛崃市| 鹿泉市| 五台县| 招远市| 亚东县| 康马县| 寿阳县| 英超| 龙泉市| 章丘市| 芦溪县| 百色市| 宜宾市| 确山县| 太仆寺旗| 侯马市| 白河县| 河源市| 方山县|