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

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

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

    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("未找到相關文件"); 
        }
     
    }
     
    //執行方法 
    public static boolean writeProperties(String filePath, String parameterName, String parameterValue) 
       
    boolean flag = false
       
    try 
           
    //取得文件所有內容 
           
    String all = CreatePro.readFile(filePath, parameterName); 
            String result 
    = null
           
    //如果配置文件里存在parameterName 
           
    if (all.indexOf(parameterName) != -1
               
    //得到parameterName前的字節數 
               
    int a=all.indexOf(parameterName); 
               
    //取得以前parameterName的值 
               
    String old = readProperties(filePath, parameterName); 
               
    //得到parameterName值前的字節數 
               
    int b=a+(parameterName.length()+"=".length()); 
               
    //新的properties文件所有內容為:舊的properties文件中內容parameterName+"="+新的parameterName值(parameterValue)+舊的properties文件中parameterName值之后的內容 
               
    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); 
    //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 
    //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 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 心。 閱讀(2476) 評論(2)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 亚洲日韩精品国产一区二区三区| 韩国免费一级成人毛片| 成年人网站在线免费观看| 99久久亚洲综合精品成人网| www免费黄色网| 亚洲Av无码乱码在线观看性色| 亚洲AV无码片一区二区三区| 日本无吗免费一二区| 亚洲国产欧美国产综合一区| 24小时免费直播在线观看| 国产精品亚洲精品| 四虎永久成人免费影院域名| 久久亚洲中文字幕无码| 国产女高清在线看免费观看| 日本高清不卡中文字幕免费| 久久精品国产亚洲5555| 国产在线精品免费aaa片| 色婷婷六月亚洲婷婷丁香| 一二三四免费观看在线视频中文版 | 亚洲理论片在线中文字幕| 在线观看的免费网站| 亚洲国产成人无码AV在线影院| 国产伦一区二区三区免费| 久久久久女教师免费一区| 亚洲色图黄色小说| 最近2019中文字幕免费看最新 | 免费99热在线观看| 国产情侣久久久久aⅴ免费| 中文字幕在线观看亚洲视频| 亚洲国产精品视频| 亚洲免费福利视频| 国产精品hd免费观看| 亚洲ts人妖网站| 国产亚洲3p无码一区二区| 无码国产精品一区二区免费式直播 | 可以免费看黄视频的网站| 一个人免费观看日本www视频| 亚洲精品无码久久久久久久| 亚洲人成网7777777国产| 韩国免费一级成人毛片| 日韩精品极品视频在线观看免费|